测量程序编制 - python 34数据类型:dict(字典)-创建(三).pptxVIP

  • 1
  • 0
  • 约小于1千字
  • 约 5页
  • 2022-04-11 发布于江西
  • 举报

测量程序编制 - python 34数据类型:dict(字典)-创建(三).pptx

dict(字典)——推导式 Dictionary(字典)——推导式 字典推导式 语法:{key_expr:value_expr for value in collection if condition} 例一: 用字典推导式求一个字符串以及其长度 strs=[study,python,come,sirt] dict1={key:len(key) for key in strs} print(dict1) {study: 5, python: 6, come: 4, sirt: 4} Dictionary(字典)——推导式 字典推导式 例二: 不区分大小写,key一样的合并 mcase={a:10,b:34,A:7,Z:3} mcase_fre={key.lower():mcase.get(key.lower(),0)+mcase.get(key.upper(),0) for key in mcase if key in [a,b]} print(mcase_fre) {a: 17, b: 34} Dictionary(字典)——推导式 字典推导式 例三:快速更换 key 和 value d={a:10,b:20} print(d) e={v:k for k,v in d.items()} print(e) {a: 10, b: 20} {10: a, 20:

文档评论(0)

1亿VIP精品文档

相关文档