练习
逐一分开字典中所有元素
a = {}
a = []
b = []
d1 = {'x': 123, 'y': 321, 'z': 566}
遍历字段d1
for(k,v) in d1.items():
print(k,v)
将遍历出来的K,存进列表a
a.append(k)
将遍历出来的v,存进列表b
b.append(v)
遍历完成打印出来
c=0
while c==2:
c= c+1
print(a)
print(b)
为了省事,将a、b两列表再转化为字典
print(dict(zip(a,b)) )
列表中取都是奇数的数字
a1 = ['一', '二', '三', '四', '五', '六', '七']
for i in range(0,len(a1),2):
print(a1[i])
顺便再比较下while吧
count = 0
while count < len(a1):
print(a1[count])
count += 2
属于a列表不属于b列表的数据,建一个c列表
a = [1,2,3,4,5,6]
b = [3,4,5]
c = []
for i in a:
if i not in b:
c.append(i)
---------------------------
print(c)
c = [1,2,6]
a、b列表中都有的元素,在a中删除
a = [1,2,3,4,5,6]
b = [3,4,5]
for i in b:
if i in a:
a.remove(i)
-----------------------
print(a)
a = [1,2,6]
网友评论