The basis of the type of supplement

⼀ added to a basic operation terminated string
column list: delete a loop elements of each column of the list ⼀
li = [ "Li Li Ka", "twist vine", "Yellow Sea ⻩ peak", "Carina '] = S' ." the Join (Li)
Print (S)
Li = "zoomed ⻩ large yellow Gui ⼥ female"
S = "
" .join (Li) Print (S)
Li = [. 11, 22 is, 33 is, 44 is]
for E in Li:
Li .remove (E)
Print (Li)
results: [22, 44]
analysis of reasons:
. when running a process for trekking ⼀ will have a pointer to the record current element which ⼀ a cycle, ⼀ a pointer to the start of the 0
Ge and then get to the 0th element. then delete the first 0. this time. the original is a first frame element will automatically automatically becomes the 0 then backward pointer moves once again, point 1 elements. At this time the original 1 has become 0, it will not be deleted a.
using the delete try to use POP:
Li = [. 11, 22 is, 33 is, 44 is]
for I in Range (0, len (Li) ):
del Li [I]
Print (Li)
results: error

i = 0, 1, 2 deleted when li [0] after being deleted. ⼀ a rear face ⾯ first becomes 0.

So when i = 2 time. List ⼀ only one element, but this is the first time to remove two certainly being given ah

The analysis found that the cycle is not deleted ⾏ trekking Using either by del Using a remove or can not be achieved then pop it....?
The only way is possible:
Or Use a column list with another ⼀ you want to record delete to delete the contents and then cycle
for EL in li:
li.pop () # POP nor does ⾏ trekking
print (li) results: [11, 22]
for i in the Range (0, len (li)): # cycle len (li) times and then delete the forward li.pop () from
Print (Li)
Li = [. 11, 22 is, 33 is, 44 is]
del_li = []
for E in Li:
del_li.append (E)
for E del_li in:
li.remove (E)
Print (li)
Note: dict in fromkey (), can help us to create a dict ⼀ list by
each column ⼀ a list of the top ⾯ plane will be used as key, after ⾯ face the contents of the column list as the value ⽣ generate dict a good note:
due to remove elements will lead to changes in the index of elements, it is easy to easy to problems as far as possible not to recycle the amount directly to delete
. in addition to the elements can be deleted another element is added to a collection ⼀ then remove bulk amounts.
DIC dict.fromkeys = ([ "Jay", "JJ"], [ "Jay", "twist vine"]) print (dic)
results:
{ 'Jay': [ 'Jay', 'twist vine'], 'JJ': [ 'Jay', 'twist vine']}

dic = dict.fromkeys ([ "jay" , "JJ"], [ " Jay", "twist vine"]) Print (DIC)
dic.get ( "Jay"). the append ( "zoomed Hu big") Print ( DIC)
results:
{ 'Jay': [ 'Jay', 'twist vine', 'Hu zoomed large'], 'JJ': [ ' Jay', 'twist vine', 'Hu zoomed big']}
tags only jay that the more you change the column list. However, due to jay and JJ use the same ⼀ use a column list. so before ⾯ that changed the face after
⾯ surface that will follow the change
element in the iterative process of dict is not admitted ⾏ trekking deleted
dic = { 'k1': ' alex', 'k2': 'wusir', 's1': ' ⾦ gold ⽼ old boss' #} with the delete key 'k' elements
for K in DIC:
IF 'K' in K:
del DIC [K]
generation when not allowed into ⾏ trekking delete
print (dic)

dictionary changed size during iteration, the loop Diego

How to do it the element to remove temporary first save in ⼀ a list, and then the cycle list, then deleted?
Dic = { 'K1': 'alex', 'K2': 'wusir', 's1': '⾦ gold ⽼ old boss'} dic_del_list = []

Remove the element with the key 'k' of

for k in dic:
if 'k' in k:
dic_del_list.append(k)
for el in dic_del_list:
del dic[el]
print(dic)

Guess you like

Origin www.cnblogs.com/gongziqianqiu/p/11012787.html