字典


主要是对字典的学习:

# 编写人:刘钰琢
# 编写日期:2021/1/1318:26
# 字典的操作字典的操作
score={'zhangsan':100,'lisi':98,'zhangmazi':56}
print(score)
print(type(score))
#第二种创建方式
std=dict(name='jack',age=20)
print(std)
#创建空字典
d={}
print(d)
#字典中元素的获取
print(score['zhangsan'])
#print(score['hhah'])#在这个中如果没有就会直接报错
print(score.get('zhangsan'))
print(score.get('maliu'))#None这是获取没有
print(score.get('dddd',99))#会输出默认值,即99
#获取所有的key
keys=score.keys()
print(keys)
print(type(keys))
print(list(keys))
#获取所有的value
values=score.values()
print(values)
print(type(values))
print(list(values))
#获取所有的key——values对
items=score.items()
print(items)
print(type(items))
print(list(items))#转换之后的列表元素是由元组(())组成
#字典元素的遍历
for item in score:
    print(item,score.get(item),score[item])
#字典
#key不允许重复
d={'name':'zhang','name':'lisi'}
print(d)
d1={'name':'zhang','dname':'zhang'}
print(d1)
#字典生成式
items=['Fruits','books','Others']
prices=[96,87,56]
d2={item.upper():price for item,price in zip(items,prices)}#.upper是将他所有的变成大写
print(d2)


文章作者: 毛豆不逗比
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 毛豆不逗比 !
  目录
{% include '_third-party/exturl.swig' %} {% include '_third-party/bookmark.swig' %} {% include '_third-party/copy-code.swig' %} + {% include '_custom/custom.swig' %}