字典相关操作

#字典详解
d={'a':'98','b':'97'}
a=dict(x=10,y=20) #创建对象
b=((('x',90),'y',100))
s1='abc'
s2='123'
print(dict(zip(s1,s2)))
print(dict.fromkeys(s1,-1))
print(a,b)
print(type(d),d)

#字典修改添加与修改
d={'a':'A','b':'B'}
d1={'a':97,'c':'C'}
d.update(d1)
print(d)

#取key 和value
for k in d.keys():
print(k,d.get(k))

for k in d.values():
print(k)
for i,j in d.items():
print(i,j)

#字符串切分列表
stocks='平安银行,17.31,16.92;万科A,32.81,31.78;全新好,9.58,9.50'
result=stocks.split(';') #['平安银行,17.31,16.92', '万科A,32.81,31.78', '全新好,9.58,9.50']
sinfo={}
keys=['high','low']
for i in result:
tmp=i.split(',')
print(tmp) #['平安银行', '17.31', '16.92'],['万科A', '32.81', '31.78'],['全新好', '9.58', '9.50']
#拼接第一个元素
sinfo[tmp[0]]=tmp[1:] # {'平安银行': ['17.31', '16.92'], '万科A': ['32.81', '31.78'], '全新好': ['9.58', '9.50']}
#拼接字典
sinfo[tmp[0]]=dict(zip(keys,tmp[1:]))
print(sinfo)

#取最高 最低值
for key in sinfo:
print(key,sinfo[key]['high'])
print(key,sinfo[key]['low'])

#将两个列表合并成一个字典操作
keys=['high','low']
valus=['32.81','31.78']
d2=(zip(keys,valus))
print(dict(d2))
posted @ 2022-05-22 17:19  人生信条~~  阅读(24)  评论(0编辑  收藏  举报