How to use the dictionary get and setdefault methods for python
Editor to share with you how python uses the dictionary get and setdefault methods. I hope you will get something after reading this article. Let's discuss it together.
Dictionary get and setdefault methods
# # navs = {} for (portfolio, equity, position) in data:if portfolio not in navs:navs [portfolio] = 0navs [portfolio] + = position * prices [equity] # # recommended that navs = {} for (portfolio, equity, position) in data:# use get method navs [portfolio] = navs.get (portfolio, 0) + position * prices [equity] # or setdefault method navs.setdefault (portfolio, 0) navs [portfolio] + = position * prices [equity] to finish reading this article I believe you have a certain understanding of "how python uses the dictionary get and setdefault method". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!