car = {'brand':'bwm','price':1000000,'color':'black'}
print(car['brand'])
print(car['price'])
print(car['color'])
bwm
1000000
black
定义:
字典名 = {'键值1':键值1值,'键值2':键值2值···}
访问:
字典名['键值名']
#每一个键值都是一个变量,相当于成员
字典['新增键值名'] = 新增键值的值
car = {'brand':'bwm','price':1000000,'color':'black'}
print(car)#字典可以直接输出
#新增型号
car['type'] = 'x765'
print(car)
{'brand': 'bwm', 'price': 1000000, 'color': 'black'}
{'brand': 'bwm', 'price': 1000000, 'color': 'black', 'type': 'x765'}
直接打印字典,元素排列顺序和定义时完全相同,用for循环遍历时也按照定义顺序。 新增键值对:会直接默认在最后。
和创建一个空列表一样
语法:
字典名 = {}
car = {'brand':'bwm','price':1000000,'color':'black'}
car['color'] = 'white'
print(car['color'])
print(car)
white
{'brand': 'bwm', 'price': 1000000, 'color': 'white'}
del 列表名['键值名'] #键值名记着加引号!!!
car = {'brand':'bwm','price':1000000,'color':'black'}
print(car)
del car['price']
print(car)
{'brand': 'bwm', 'price': 1000000, 'color': 'black'}
{'brand': 'bwm', 'color': 'black'}
car = {'brand':'bwm',
'price':1000000,
'color':'black'}
print(car)
#竖着这样写可以让键值对更清晰
{'brand': 'bwm', 'price': 1000000, 'color': 'black'}
car = {'brand':'bwm','price':1000000,'color':'black'}
print(car.get('brand'))
print(car.get('owner'))
bwm
None
语法:
for 键值变量an,键值值bn in 列表名.items():
执行块
#注意!!! 列表名.items()不可以省略。items!!!
car = {'brand':'bwm','price':1000000,'color':'black'}
for key,value in car.items():
print(f"\nKey: {key.title()}")
print(f"\nValue: {value}")
Key: Brand
Value: bwm
Key: Price
Value: 1000000
Key: Color
Value: black
for 键值变量an in 列表名.keys():
执行块
.keys()是让前面的变量对应键值名变量。(访问以键值名构成的列表)
car = {'brand':'bwm','price':1000000,'color':'black'}
for key in car.keys():
print(f"\nKey: {key.title()}")
Key: Brand
Key: Price
Key: Color
for 键值变量an in 列表名.values():
执行块
.values()是让前面的变量对应键值。(访问以键值构成的列表)
car = {'brand':'bwm','price':1000000,'color':'black'}
for value in car.values():
print(f"\nKey: {value}")
Key: bwm
Key: 1000000
Key: black
#想按照首字母排序 打印:键值名变量 用sorted()临时排序
car = {'brand':'bwm','price':1000000,'color':'black'}
for key in sorted(car.keys()):
print(f"\nKey: {key.title()}")
Key: Brand
Key: Color
Key: Price
favo_language = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python'
}
print("The following languages have been mentioned:")
for language in set(favo_language.values()):
print(language.title())
The following languages have been mentioned:
Ruby
C
Python
#对列表去重处理: set(列表名)
car_1 = {'color':'black','price':3000}
car_2 = {'color':'whilt','price':34000}
car_3 = {'color':'yellow','price':342000}
cars = [car_1,car_2,car_3] #注意这里列表用的是方括号
print(cars)
for car in cars:
print(car)
[{'color': 'black', 'price': 3000}, {'color': 'whilt', 'price': 34000}, {'color': 'yellow', 'price': 342000}]
{'color': 'black', 'price': 3000}
{'color': 'whilt', 'price': 34000}
{'color': 'yellow', 'price': 342000}
#创建多个的时候可以使用for() 和 range()
aliens = []
for alien_number in range(0,5):
new_alien = {'color':'green','speed':'slow'}
aliens.append(new_alien)
for alien_number in range(5,30):
new_alien = {'color':'yellow','speed':'fast'}
aliens.append(new_alien)
for alien in aliens[0:30:5]:
print(alien)
print(len(aliens))
{'color': 'green', 'speed': 'slow'}
{'color': 'yellow', 'speed': 'fast'}
{'color': 'yellow', 'speed': 'fast'}
{'color': 'yellow', 'speed': 'fast'}
{'color': 'yellow', 'speed': 'fast'}
{'color': 'yellow', 'speed': 'fast'}
30
pizza = {
'crust':'thick',
'toppings':['mushrooms','extra cheese'],
}
for topping in pizza.get('toppings'): #也可以写成 pizza['toppingsb']
print(topping)
mushrooms
extra cheese
pizza = {
'toppings':{'first':'mushrooms','second':'extra cheese'}
}
for sequence,topping in pizza['toppings'].items():
print(f"{sequence}: {topping}")
first: mushrooms
second: extra cheese