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'}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM