Data type of operation common problems

Add a list of cycle

lst = [1,2,3,4,5,6]
for i in lst:
    lst.append(7) # 这样写法就会一直持续添加7
    print(lst)
print(lst)

A list of cycle remove the error examples

List: circulation delete the list each element ⼀

li = [11, 22, 33, 44]
for e in li:
    li.remove(e)
print(li)
结果:
[22, 44]

Analyze the reasons:. For the process will be when running a ⼀ pointer to record the elements of the current cycle in which a ⼀, ⼀ pointer to start the first 0.

Then get to the 0th element. Then delete the first 0. This time. Turned out to be one of the first frame element will automatically become the first 0.

Then move the pointer back ⼀ times, point 1 elements. In this case the original one has become zero, there will not be deleted.

li = [11, 22, 33, 44]
for i in range(0, len(li)):
    del li[i]
print(li)
结果: 报错
# 删除的时候li[0] 被删除之后. 后⾯⼀个就变成了第0个.
# 以此类推. 当i = 2的时候. list中只有⼀个元素. 但是这个时候删除的是第2个 肯定报错啊

The analysis found. ⾏ not delete cycle. Use del whether or Use remove. Can not be realized. Then pop it?

Try using pop delete

for el in li:
 li.pop() # pop也不⾏
print(li)
结果:
[11, 22]

A list of cycle deleted successfully

Only in this way it is possible:

for i in range(0, len(li)): # 循环len(li)次, 然后从后往前删除
 li.pop()
print(li)
或者. ⽤另⼀个列表来记录你要删除的内容. 然后循环删除

li = [11, 22, 33, 44]
del_li = []
for e in li:
 del_li.append(e)
for e in del_li:
 li.remove(e)
print(li)

li = [1,2,3,4]
lst = li[:]
for i in lst:
    li.remove(i)
print(li)

Note: Due to remove elements will lead to changes in the index of elements, so prone to problems as far as possible not to recycle directly to delete elements can be added to the element to remove the container for another time and then bulk delete...

Dictionary pit

dict in fromkey (), revert again to help us to create a dict ⼀ by list

dic = dict.fromkeys(["jay", "JJ"], ["周杰伦", "麻花藤"])
print(dic)
结果:
{'jay': ['周杰伦', '麻花藤'], 'JJ': ['周杰伦', '麻花藤']}

Just change the code jay that list. However, due to jay and JJ Using the same ⼀ lists. So before ⾯ that changed. Behind the change to follow 

The application does not modify the size of the data in the loop

dict the elements are not allowed into the ⾏ in the iterative process 添加and 删除the

dic = {'k1': 'alex', 'k2': 'wusir', 'k3': '大宝哥'}
# 删除key中带有'k'的元素
for k in dic:
  if 'k' in k:
 del dic[k] # dictionary changed size during iteration, 在循环迭
代的时候不允许进⾏删除操作
print(dic)

How to do it? The elements to be deleted before being saved in ⼀ a list, and then the cycle list, then deleted

dic = {'k1': 'alex', 'k2': 'wusir', 'k3': '大宝哥'}
dic_del_list = []
# 删除key中带有'k'的元素
for k in dic:
     if 'k' in k:
     dic_del_list.append(k)

for el in dic_del_list:
     del dic[el]
print(dic)

# 使用两个字典进行删除
dic = {'k1': 'alex', 'k2': 'wusir', 'k3': '大宝哥'}
dic1 = dic.copy()
for i in dic1:
    dic.pop(i)
print(dic)

Data set can not be modified in the loop size

set1 = {1,2,3,4,5,6}
for i in set1:
    set1.pop()
print(set1)

Traceback (most recent call last):
  File "/python项目/m2.py", line 224, in <module>
    for i in set1:
RuntimeError: Set changed size during iteration

Successfully deleted

set1 = {1,2,3,4,5,6}
set2 = set1.copy()
for i in set2:
    set1.remove(i)
print(set1)

Type Conversion

元组 => 列表 list(tuple)
列表 => 元组 tuple(list)
list=>str str.join(list)
str=>list str.split()

转换成False的数据:
0,'',None,[],(),{},set() ==> False

Guess you like

Origin www.cnblogs.com/luckinlee/p/11619991.html