Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of element access examples of python Dictionary

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "element access case Analysis of python Dictionary". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Description

1. There is no concept of subscript in the dictionary. Use the key value to access the corresponding value in the dictionary. The code reports an error when the accessed key value does not exist.

2. Get ("key"): pass the key value directly into the function. When the corresponding value is queried, the corresponding value is returned. When the key value does not exist, None is returned. The code will not make an error.

3. Get (key, data): when querying the corresponding value, the corresponding value is returned, and when there is no key value, the custom data value is returned.

Example # define a dictionary dic = {"Name": "Zhang San", "Age": 20} # use key value to access element print (dic ["Name") # use get () to access element print (dic.get ("Name")) print (dic.get ("Height")) print (dic.get ("Height", 178))

Knowledge point expansion:

Traversal dictionary:

1. Use the dict.items () method of the dictionary object to get the meta-ancestor list of each element of the dictionary, that is, the key-value pair:

Dict = {1: 1,2: "aa", "D": "ee", "Ty": 45} for item in dict.items (): print (item) output: (1,1) (2, "aa") ("D", "ee") ("Ty", 45)

two。 Get each specific key and value:

"" No one answers any questions? the editor has created a Python learning exchange QQ group: 531509025 find like-minded partners, help each other, and there are good video learning tutorials and PDF e-books in the group! " Dict = {1: 1,2: "aa", "D": "ee", "Ty": 45} for key, value in dict.items (): print (key, value)

Output:

1 1

2 aa

D ee

Ty 45

3. You can also use the keys () and values () methods to get a list of keys and values for the dictionary:

Dict = {1: 1,2: "aa", "D": "ee", "Ty": 45} for key in dict.keys (): print (key) for value in dict.values (): print (value)

Output:

one

two

D

Ty

one

Aa

Ee

forty-five

Phellodendron mandshurica (Thunb.)

This is the end of "element access instance Analysis of python Dictionary". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report