【Python编程:从入门到实践】字典(dictionary)

字典

  • 键-值对
    • 键只能字符串
    • 值可以是python的各种对象
  • 修改字典
    • 修改值
    • del 删除键值对
  • 遍历字典
    • 遍历键值对 for keyword,value in dict.items()
    • 遍历键 for keyword in dict.keys()
    • 遍历值 for value in dict.values()
    • 按顺序遍历 sorted()
    • 遍历值,去重复项 set() 每个数据独一无二
  • 字典列表 [{},{},{},…]
  • 字典中存储列表
  • 字典嵌套字典

6.1 alien_demo

alien_0 = {'color':'red','height':180}
print(alien_0['color'])#red

#添加键值对
alien_0['x_position'] = 20
alien_0['y_position'] = 40
print(alien_0)
#{'color': 'red', 'height': 180, 'x_position': 20, 'y_position': 40}

alien_1 = {}
alien_1['color'] = 'green'
alien_1['points'] = 10
print(alien_1)
#{'color': 'green', 'points': 10}

6.2 修改字典

# 修改
alien_3 = {'color': 'red', 'height': 180, 'x_position': 20, 'y_position': 40}
alien_3['color'] = 'red'
alien_3['height'] = alien_3['height'] + 5
print(alien_3)
#{'color': 'red', 'height': 185, 'x_position': 20, 'y_position': 40}

# 删除 del
del alien_3['color']
print(alien_3)
#{'height': 185, 'x_position': 20, 'y_position': 40}

6.3 对象字典

favorite_fruits = {
    'luowanyu':'mango',
    'luosiyu':'banana',
    'likaihong':'mango',
    'lisiying':'orange',
}
print('luosiyu favorite fruit is '+ favorite_fruits['luosiyu'].upper())
#luosiyu favorite fruit is BANANA

6.4 遍历字典

# 遍历键值对
alien_3 = {'color': 'red', 'height': 180, 'x_position': 20, 'y_position': 40}
for item,value in alien_3.items():
    print(item+":"+str(value))
# color:red
# height:180
# x_position:20
# y_position:40

#遍历键
for keyword in alien_3.keys():
    print(keyword)

#遍历值
for value in alien_3.values():
    print(value)
    ```
```python
# 6.5 按顺序遍历字典中的所有键
alien_3 = {'color': 'red', 'height': 180, 'x_position': 20, 'y_position': 40}
for key in sorted(alien_3.keys()):
    print(key)
# color
# height
# x_position
# y_position

6.6 遍历值,去除重复项

favorite_fruits = {
    'luowanyu':'mango',
    'luosiyu':'banana',
    'likaihong':'mango',
    'lisiying':'orange',
}
for value in set(favorite_fruits.values()):
    print(value)
# banana
# mango
# orange

6.7 字典列表

people_1 = {
    'name' : 'zhoujielun',
    'age' : 45
}
people_2 = {
    'name' : 'linjunjie',
    'age' : 40
}
people_3 = {
    'name' : 'guofucheng',
    'age' : 42
}
peoples = [people_1,people_2,people_3]
print(peoples)

for item in peoples[-2:]:
    print(item)

6.8 字典中存储列表

people = {
    'name' : 'libinglin',
    'age' : 22,
    'address' : ['sichuan','hainan','xizang'],
    'score' : [100,90,98,92],
}
#print(people)
#{'name': 'libinglin', 'age': 22, 'address': ['sichuan', 'hainan', 'xizang'], 'score': [100, 90, 98, 92]}

print("people's name is "+people['name'])
for adr in people['address']:
    print(people['name']+'居住过'+adr)
# people's name is libinglin
# libinglin居住过sichuan
# libinglin居住过hainan
# libinglin居住过xizang

6.9 最喜欢的语言

favorite_lage = {
    'li':['python','cpp','c'],
    'luo':['javascript','java'],
    'zhang':['python','go','java'],
    'long':['cpp']
}
for name,languages in favorite_lage.items():
    if len(languages)>1:
        print(name.title()+" favorate languages are:")
        for lang in languages:
            print('\t'+lang)
    else:
        print(name.title()+' favorate languages is:'+"\n\t"+languages[0])
# Li favorate languages are:
# 	python
# 	cpp
# 	c
# Luo favorate languages are:
# 	javascript
# 	java
# Zhang favorate languages are:
# 	python
# 	go
# 	java
# Long favorate languages is:
# 	cpp

6.10 字典嵌套字典

cities = {
    'chengdu':{
        'population':20000000,
        'revenue':'2万亿',
        'area':'二万平方公里'
    },
    'nanjing':{
        'population':10000000,
        'revenue':'1.5万亿',
        'area':'一万平方公里',
    },
    'beijing':{
        'population':30000000,
        'revenue':'4万亿',
        'area':'五万平方公里',
    }
}
for name,value in cities.items():
    print(name)
    print(value)
# chengdu
# {'population': 20000000, 'revenue': '2万亿', 'area': '二万平方公里'}
# nanjing
# {'population': 10000000, 'revenue': '1.5万亿', 'area': '一万平方公里'}
# beijing
# {'population': 30000000, 'revenue': '4万亿', 'area': '五万平方公里'}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值