程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Dictionary (1)

編輯:Python

1 Create a dictionary

A dictionary is a series of key value pairs , Each key corresponds to a value , Values can be numbers 、 character string 、 list 、 Dictionary, etc

# Create a dictionary , If you need to create an empty dictionary students_info={}
students_info={'jack':5,'andy':7,'kiki':6,'alicy':8}
# Interview student age
print(students_info.keys())
print(students_info.values())
print(f"Andy is {students_info['andy']} years old.")

Running results :

dict_keys(['jack', 'andy', 'kiki', 'alicy'])
dict_values([5, 7, 6, 8])
Andy is 7 years old.
 

2 Dictionary operation

# Dictionary operation
students_info={'jack':5,'andy':7,'kiki':6,'alicy':8}
students_info['allen']=7# Add key value pair
print(students_info)
students_info['allen']=18# modify allen The associated value is changed to 18
print(students_info)
del students_info['allen']# Delete allen Key value pair
print(students_info)

Running results :
{'jack': 5, 'andy': 7, 'kiki': 6, 'alicy': 8, 'allen': 7}
{'jack': 5, 'andy': 7, 'kiki': 6, 'alicy': 8, 'allen': 18}
{'jack': 5, 'andy': 7, 'kiki': 6, 'alicy': 8}

3 Dictionary access

(1)get visit

# Dictionary access
students_info={'jack':5,'andy':7,'kiki':6,'alicy':8}
# Visit the dictionary , If there is no such key value , return No this student, If the second parameter is not specified , return None
student_age=students_info.get('andy', 'No this student.')
print(f"Andy is {student_age} years old.")

Running results :
Andy is 7 years old.

(2) Traversal access

# Create a dictionary
students_info={'jack':5,'andy':7,'kiki':6,'alicy':8}
# Visit the dictionary , If there is no such key value , return No this student, If the second parameter is not specified , return None
student_age=students_info.get('andy', 'No this student.')
print(f"Andy is {student_age} years old.")
# Traversal key value pairs
students_info={'jack':5,'andy':7,'kiki':7,'alicy':8}
# Traverse all keys and values
for key,value in students_info.items():
print(f"{key} is {value} years old.")
# Traverse all keys , Traverse after temporary sorting
for name in sorted(students_info.keys()):
print(f"student name is {name}")
# Traverse all values
for age in students_info.values():
print(age)
# Check age
students_name=['jack','kiki']
for name in students_name:
age=students_info[name]
print(f"{name} is {age} years old.")
# List age groups that do not repeat ,set
for age in set(students_info.values()):
print(f" The age of non repetition is :{age}")
# Judge allen Is there age information on record
if 'allen' not in students_info.keys():
print("Allen`s age is not in the table. \nPlease give information.")

Running results :

Andy is 7 years old.
jack is 5 years old.
andy is 7 years old.
kiki is 7 years old.
alicy is 8 years old.
student name is alicy
student name is andy
student name is jack
student name is kiki
5
7
7
8
jack is 5 years old.
kiki is 7 years old.
The age of non repetition is :8
The age of non repetition is :5
The age of non repetition is :7
Allen`s age is not in the table.
Please give information.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved