current position:Home>Python dictionary query key value pair methods and examples
Python dictionary query key value pair methods and examples
2022-02-01 20:12:33 【Parson old bird】
Finding is the focus of all our data type learning , Dictionaries are no exception , Find... From different dimensions in different ways , have everything that one expects to find . Let's learn from simple to difficult step by step Query method of dictionary .
1. Use the key to find a single value ****
dict5 = {'name': 'Tom', 'age': 18, 'love': 'python'}
print(dict5['age']) # Use the key to find the value
Return results :18
Copy code
2. Use get() Method to find the value corresponding to the key ****
dict5 = {'name': 'Tom', 'age': 18, 'love': 'python'}
print(dict5.get('age'))
# utilize get The function uses the key to find the value , If key There is no return None
print(dict5.get('age', ' The default value is '))
# If the key doesn't exist , Set to return to default
Return results :18
Copy code
3. Use setdefault() lookup ****
setdefault and get The function is almost , But if the key doesn't exist , Then the search content is put into the dictionary as a key , And set the default value , Not set to None
dict5 = {'name': 'Tom', 'age': 18, 'love': 'python'}
print(dict5.setdefault('age2'))
age2 non-existent , return None
Copy code
4. Query all keys and values and key value pairs ****
dict5 = {'name': 'Tom', 'age': 18, 'love': 'python'}
# keys() # Get all keys , Returns a list of
print(dict5.keys())
# values() # Get all values , Returns a list of
print(dict5.values())
# items() # Get all key value pairs , Returns the list of Yuanzu
print(dict5.items())
Return results :
dict_keys(['name', 'age', 'love', 'age2'])\
dict_values(['Tom', 18, 'python', None])\
dict_items([('name', 'Tom'), ('age', 18), ('love', 'python'), ('age2', None)])
Copy code
From the above returned results, we find that there are three different data types :dict_keys(),dict_values(),dict_items(), Such data cannot be accessed according to the list subscript .
v = dict5.values()
print(v[1])
Return results :
TypeError: 'dict_values' object is not subscriptable
Copy code
At this time, we can change to Yuanzu and then visit
list_v = list(v)
print(list_v[1])
Return results :18
Copy code
copyright notice
author[Parson old bird],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202012012321095.html
The sidebar is recommended
- Python data analysis - linear regression selection fund
- How to make a python SDK and upload and download private servers
- Python from 0 to 1 (day 20) - basic concepts of Python dictionary
- Django -- closure decorator regular expression
- Implementation of home page and back end of Vue + Django tourism network project
- Easy to use scaffold in Python
- [Python actual combat sharing] I wrote a GIF generation tool, which is really TM simple (Douluo continent, did you see it?)
- [Python] function decorators and common decorators
- Explain the python streamlit framework in detail, which is used to build a beautiful data visualization web app, and practice making a garbage classification app
- Construction of the first Django project
guess what you like
-
Python crawler actual combat, pyecharts module, python realizes the visualization of river review data
-
Python series -- web crawler
-
Plotly + pandas + sklearn: shoot the first shot of kaggle
-
How to learn Python systematically?
-
Analysis on several implementations of Python crawler data De duplication
-
leetcode 1616. Split Two Strings to Make Palindrome (python)
-
Python Matplotlib drawing violin diagram
-
Python crawls a large number of beautiful pictures with 10 lines of code
-
[tool] integrated use of firebase push function in Python project
-
How to use Python to statistically analyze access logs?
Random recommended
- How IOS developers learn Python Programming 22 - Supplement 1
- Python can meet any API you need
- Python 3 process control statement
- The 20th of 120 Python crawlers, 1637. All the way business opportunity network joined in data collection
- Datetime of pandas time series preamble
- How to send payslips in Python
- [Python] closure and scope
- Application of Python Matplotlib color
- leetcode 1627. Graph Connectivity With Threshold (python)
- Python thread 08 uses queues to transform the transfer scenario
- Python: simple single player strange game (text)
- Daily python, chapter 27, Django template
- TCP / UDP communication based on Python socket
- Use of pandas timestamp index
- leetcode 148. Sort List(python)
- Confucius old book network data collection, take one anti three learning crawler, python crawler 120 cases, the 21st case
- [HTB] cap (datagram analysis, setuid capability: Python)
- How IOS developers learn Python Programming 23 - Supplement 2
- How to automatically identify n + 1 queries in Django applications (2)?
- Data analysis starts from scratch. Pandas reads HTML pages + data processing and analysis
- 1313. Unzip the coding list (Java / C / C + + / Python / go / trust)
- Python Office - Python edit word
- Collect it quickly so that you can use the 30 Python tips for taking off
- Strange Python strip
- Python crawler actual combat, pyecharts module, python realizes China Metro data visualization
- DOM breakpoint of Python crawler reverse
- Django admin custom field stores links in the database after uploading files to the cloud
- Who has powder? Just climb who! If he has too much powder, climb him! Python multi-threaded collection of 260000 + fan data
- Python Matplotlib drawing streamline diagram
- The game comprehensively "invades" life: Python releases the "cool run +" plan!
- Python crawler notes: use proxy to prevent local IP from being blocked
- Python batch PPT to picture, PDF to picture, word to picture script
- Advanced face detection: use Dlib, opencv and python to detect face markers
- "Python 3 web crawler development practice (Second Edition)" is finally here!!!!
- Python and Bloom filters
- Python - singleton pattern of software design pattern
- Lazy listening network, audio novel category data collection, multi-threaded fast mining cases, 23 of 120 Python crawlers
- Troubleshooting ideas and summary of Django connecting redis cluster
- Python interface automation test framework (tools) -- interface test tool requests
- Implementation of Morse cipher translator using Python program