python中提取字典的指定键值对
>>> test1 = dict(key1="xx",key2="yy",key3="aa",key4="bb",key5="cc",key6="dd")
>>> type(test1)
<class 'dict'>
>>> len(test1)
6
>>> test2=["key3","key5"]
>>> for i in test2:
print(i,test1[i])
key3 aa
key5 cc
>>> test3 = {}
>>> for i in test2:
test3[i] = test1[i]
>>> test3
{'key3': 'aa', 'key5': 'cc'}
>>> j = 0
>>> test4 = {}
>>> for i in test1.keys():
j = j + 1
if j % 2 == 0:
test4[i] = test1[i]
>>> test4
{'key2': 'yy', 'key4': 'bb', 'key6': 'dd'}