Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the dictionary get and setdefault methods for python

Shulou Source: shulou.com Published: 2022-06-01 04:08:38 10月03日 Update

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!

Tags: Methods dictionaries articles recommendations done more knowledge industry information information channels channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi OPPO Reno MariaDB Shulou Tech Info Docker