Python dictionaries

Python สำหรับผู้ใช้ MATLAB

Justin Kiggins

Product Manager

Dictionary คืออะไร?

  • คล้ายกับ structure array ใน MATLAB
  • คอลเลกชันของคู่ key:value
  • ไม่รับประกันลำดับ
Python สำหรับผู้ใช้ MATLAB

การดึงข้อมูลจาก dictionary

type(definitions)
dict
print(definitions['aardvark'])
A nocturnal burrowing mammal with long ears, a tubular snout, and a long 
extensible tongue, feeding on ants and termites. Aardvarks are native to 
Africa and have no close relatives.
Python สำหรับผู้ใช้ MATLAB

การสร้าง dictionary

# Create a dictionary
dog = {'name': 'Toby', 'breed': 'Basset Hound'}
Python สำหรับผู้ใช้ MATLAB

การเพิ่มข้อมูลใน dictionary

# Add more key:value pairs
dog['weight (lbs)'] = 552.3
dog['birthdate'] = "2016-06-26"
print(dog)
{'name': 'Toby',
 'breed': 'Basset Hound',
 'weight (lbs)': 552.3, 
 'birthdate': '2016-06-26'}
Python สำหรับผู้ใช้ MATLAB

การอัปเดต dictionary

# Update weight value
dog['weight (lbs)'] = 52.3

print(dog)
{'name': 'Toby',
 'breed': 'Basset Hound',
 'weight (lbs)': 52.3,
 'birthdate': '2016-06-26'}
Python สำหรับผู้ใช้ MATLAB

การลบข้อมูลออกจาก dictionary

dog.pop('birthdate')
print(dog)
{'name': 'Toby',
 'breed': 'Basset Hound',
 'weight (lbs)': 52.3}
Python สำหรับผู้ใช้ MATLAB

มาฝึกกันเถอะ!

Python สำหรับผู้ใช้ MATLAB

Preparing Video For Download...