关键词搜索

源码搜索 ×
×

Python工作笔记-dictionary的遍历以及enumerate使用以及Py3中has_key的替代

发布2018-10-09浏览4792次

详情内容

程序运行截图如下:

源码如下:

  1. if __name__ == '__main__':
  2. diction={'1' : 'test1', '2' : 'test2', '3' : 'test3', '4' : 'testA'}
  3. hasKey={'2' : 'NiMei'}
  4. print("\nThe diction keys are ", end = " ")
  5. print(diction.keys())
  6. print("The hasKey keys are ", end = " ")
  7. print(hasKey.keys(), end="\n\n")
  8. i=0
  9. for element in diction:
  10. print( "i:" + str(i) + "\t" + "element:" + element + "\t" + "diction[element]:" +diction[element])
  11. i += 1
  12. pass
  13. print("\n\n")
  14. for j, element in enumerate(diction):
  15. print("j:" + str(j) + "\t" + "element:" +element + "\t" + "diction[j]" + diction[str(j+1)])
  16. pass
  17. if '2' in hasKey:
  18. print("hasKey.has_key('2') is true !")
  19. else:
  20. print("hasKey.has_key('2') is false !")
  21. if '1' in hasKey:
  22. print("hasKey.has_key('1') is true !")
  23. else:
  24. print("hasKey.has_key('1') is false !")
  25. pass

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载