Python 字典

面向 MATLAB 用户的 Python

Justin Kiggins

Product Manager

什么是字典?

  • 类似 MATLAB 的结构体数组
  • 键:值 对的集合
  • 不保证有序
面向 MATLAB 用户的 Python

从字典中取数

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.
面向 MATLAB 用户的 Python

创建字典

# 创建字典
dog = {'name': 'Toby', 'breed': 'Basset Hound'}
面向 MATLAB 用户的 Python

向字典添加数据

# 添加更多键:值
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'}
面向 MATLAB 用户的 Python

更新字典

# 更新体重值
dog['weight (lbs)'] = 52.3

print(dog)
{'name': 'Toby',
 'breed': 'Basset Hound',
 'weight (lbs)': 52.3,
 'birthdate': '2016-06-26'}
面向 MATLAB 用户的 Python

从字典中删除数据

dog.pop('birthdate')
print(dog)
{'name': 'Toby',
 'breed': 'Basset Hound',
 'weight (lbs)': 52.3}
面向 MATLAB 用户的 Python

开始练习!

面向 MATLAB 用户的 Python

Preparing Video For Download...