零基础学Python--你应该知道的字典小技巧
在处理数据方面,使用的最多的数据类型应该就是字典了。有同学要说了Pandas和Numpy不是使用的更多吗?是的,不过它们并不是一种数据类型,而是Python的常用包而已。
字典一方面可以很方便的进行各种操作,另一方面还可以很好的和Json结合起来使用,很多的云服务的返回格式都是json的格式,因此非常便于我们去衔接起来。
当然在实际数据处理的过程中,我们使用Pandas会更加的方便,使用的频率也更加的高,后面我们也会专题来介绍Pandas。Pandas的强大功能可以和各种文件格式类型很好的接轨,在很多的局部数据处理我们用到字典的地方还是比较多的,特别是一些小数据的处理上。接下来我们来看看字典到底有哪些好用的小技巧。
通过两个列表创建字典
keys = ['Date', 'Open', 'Close']
values = [20190318, 10.08, 10.68]
myDict = dict(zip(keys, values))
print(myDict)
运行结果:
{'Date': 20190318, 'Open': 10.08, 'Close': 10.68}
通过赋值的方式来创建字典
myDict = dict(Date=20190318, Open=10.08, Close=10.68)
print(myDict)
运行结果:
{'Date': 20190318, 'Open': 10.08, 'Close': 10.68}
通过元组列表来创建字典
tupleList = [('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
myDict = dict(tupleList)
print(myDict)
运行结果:
{'Date': 20190318, 'Open': 10.08, 'Close': 10.68}
判断Key是否在字典中
tupleList = [('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
myDict = dict(tupleList)
print('High' is in myDict)
print('Close' is in myDict)
运行结果:
False
True
获取字典某个Key对应的Value
当然我们可以直接用:
tupleList = [('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
myDict = dict(tupleList)
print(myDict['Date'])
也可以用:
date = myDict.get('Date')
print(date)
运行结果:
20190318
修改字典的Value
tupleList = [('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
myDict = dict(tupleList)
myDict['Close'] = 10.88
print(myDict)
运行结果:
{'Date': 20190318, 'Open': 10.08, 'Close': 10.88}
修改多个Key值
tupleList = [('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
myDict = dict(tupleList)
myDict.update({'Open': 10.18, 'Close': 10.88})
print(myDict)
运行结果:
{'Date': 20190318, 'Open': 10.18, 'Close': 10.88}
从字典中删除一个Key值
tupleList = [('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
myDict = dict(tupleList)
del myDict['Close']
print(myDict)
运行结果:
{'Date': 20190318, 'Open': 10.08}
我们也可以用:
tupleList = [('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
myDict = dict(tupleList)
myDict.pop('Close')
print(myDict)
运行结果:
{'Date': 20190318, 'Open': 10.08}
删除字典中不知是否存在的Key值
如果我们去删除一个不存的Key,那么会报错,但我们可以用如下方法来避免额外增加判断代码。
tupleList = [('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
myDict = dict(tupleList)
myDict.pop('High', None)
print(myDict)
运行结果:
{'Date': 20190318, 'Open': 10.08, 'Close': 10.68}
删除一组Key值
tupleList = [('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
myDict = dict(tupleList)
list(map(lambda x : myDict.pop(x, None),['Open', 'High', 'Close']))
print(myDict)
运行结果:
{'Date': 20190318}
遍历字典
tupleList = [('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
myDict = dict(tupleList)
for k,v in myDict.items():
print(k,v)
运行结果:
Date 20190318
Open 10.08
Close 10.68
当然,我们也可以用:
tupleList = [('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
myDict = dict(tupleList)
for k in myDict:
print(k, myDict[k])
或者:
tupleList = [('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
myDict = dict(tupleList)
for k in iter(myDict):
print(k, myDict[k])
或者:
tupleList = [('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
myDict = dict(tupleList)
for k in myDict.keys():
print(k, myDict[k])
运行结果都是一样的。
字典转为元组列表
tupleList = [('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
myDict = dict(tupleList)
dictTupleList = list(myDict.items())
print(dictTupleList)
其结果为:
[('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
为字典添加新的Key值
tupleList = [('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
myDict = dict(tupleList)
myDict['High'] = 10.88
print(myDict)
运行结果:
{'Date': 20190318, 'Open': 10.08, 'Close': 10.68, 'High': 10.88}
为字典添加一组Key值
tupleList = [('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
myDict = dict(tupleList)
myDict.update({'High': 10.88, 'Low': 9.98})
print(myDict)
运行结果:
{'Date': 20190318, 'Open': 10.08, 'Close': 10.68, 'High': 10.88, 'Low': 9.98}
获得固定排序的字典
from collections import OrderedDict
tupleList = [('Date', 20190318), ('Open', 10.08), ('Close', 10.68)]
myDict = dict(tupleList)
myOrderedDict = OrderedDict(sorted(myDict.items()))
print(myOrderedDict)
运行结果:
OrderedDict([('Close', 10.68), ('Date', 20190318), ('Open', 10.08)])
看起来有点不一样对吧,那就是为什么它能排序的原因,字典类型本身是乱序的。
到这里,我们关于字典的使用就介绍完了。大家可以看到,还是比较简单易用的。在实际的Python的使用中,会有很多的各种类型的嵌套用法,也不难。所谓优秀的程序员,在掌握了好的方法的前提下,无非是读的代码多,写的代码也多,这两者缺一不可。
人工智能与深度学习做量化请关注:AI量化(https://t.zsxq.com/RvfY37y) 星球限时免费,如需加入,请私信我获得免费邀请码!
零基础学习Python与深度学习应用请关注星球:Python与深度学习 https://t.zsxq.com/bUFayZ3
微信公众号:QTechAI