python学习笔记10(字典列表,在字典里面添加字典、列表)
1.字典列表:#字典列表alien0={'name':'huahua1','age':11}alien1={'name':'huahua2','age':12}alien2={'name':'huahua3','age':13}alien=[alien0,alien1,alien2]print('alien字典列表:')print(alien)#回顾如何利用range()函数生成列表alien=[
·
1.字典列表:
#字典列表
alien0={'name':'huahua1','age':11}
alien1={'name':'huahua2','age':12}
alien2={'name':'huahua3','age':13}
alien=[alien0,alien1,alien2]
print('alien字典列表:')
print(alien)
#回顾如何利用range()函数生成列表
alien=[]
for name in range(1,7,2):#生成1-6之内的奇数
alien.append(name)
print(alien)
#利用range()函数随机生成多个字典
alien=[]#生成一个空列表
for alien_number in range(0,6):#生成6个元素
new_alien={'name':'huahua1','age':11}
alien.append(new_alien)#向空字典里面添加元素
print('字典列表:')
print(alien)
print('输出字典列表长度:')
print(len(alien))
print('输出列表中的后3个字典元素:')
for name in alien[-3:]:#利用切片输出后3个元素
#for name in alien[:3]可以实现输出前3个元素
print(name)
for name in alien[:]:#遍历alien列表,里面的元素是字典,将元素存入name里面,这里的name是个字典
if name['name']=='huahua1':#访问字典的键值对
name['age']=12#修改字典键值对
print(alien)
alien字典列表:
[{'name': 'huahua1', 'age': 11}, {'name': 'huahua2', 'age': 12}, {'name': 'huahua3', 'age': 13}]
[1, 3, 5]
字典列表:
[{'name': 'huahua1', 'age': 11}, {'name': 'huahua1', 'age': 11}, {'name': 'huahua1', 'age': 11}, {'name': 'huahua1', 'age': 11}, {'name': 'huahua1', 'age': 11}, {'name': 'huahua1', 'age': 11}]
输出字典列表长度:
6
输出列表中的后3个字典元素:
{'name': 'huahua1', 'age': 11}
{'name': 'huahua1', 'age': 11}
{'name': 'huahua1', 'age': 11}
[{'name': 'huahua1', 'age': 12}, {'name': 'huahua1', 'age': 12}, {'name': 'huahua1', 'age': 12}, {'name': 'huahua1', 'age': 12}, {'name': 'huahua1', 'age': 12}, {'name': 'huahua1', 'age': 12}]
Process finished with exit code 0
2.字典嵌套列表:
student={
'name':'huahua',
'hobbies':['reading','writing','drawing'],
'age':12,
}
print('请输出学生字典里面记录的信息:')
for key,value in student.items():
print(key)
print(value)
#修改列表内元素信息
for key in sorted(student.keys()):
if key=='hobbies':
student[key].append('playing football')
print('修改后的字典:')
print(student)
#定义一个爱好列表
hobbies=['reading','studing','watching Tv']
#判断列表内的哪些元素是huahua的爱好
for hobby in hobbies[:]:#按照切片方式访问列表
if hobby in student['hobbies']:
print(hobby.title()+' is '+student['name']+"'s hobby.")
#判断huahua的爱好哪些是列表内的元素
for hobby in student['hobbies']:
if hobby in hobbies:
print(hobby.title())
请输出学生字典里面记录的信息:
name
huahua
hobbies
['reading', 'writing', 'drawing']
age
12
修改后的字典:
{'name': 'huahua', 'hobbies': ['reading', 'writing', 'drawing', 'playing football'], 'age': 12}
Reading is huahua's hobby.
Reading
Process finished with exit code 0
3.字典嵌套字典:
#字典嵌套字典,字典之间用逗号隔开,同时注意缩进
user={
'Daming':{
'age':12,
'hobbies':['reading','writing','drawing'],
'location':'China',
},
'Lingling':{
'age':22,
'hobbies':['reading','writing','watching Tv'],
'location':'England',
},
}
print(user)
#定义一个爱好列表
hobbies=['reading','writing','watching Tv','playing football']
print('the original list of hobbies:')
print(hobbies)
for user_name,user_info in user.items():#将用户名,用户信息分别存储到user_name,user_info中,其中user_info记录了字典
if user_name=='Daming':
print('It is the infomation of '+user_name+':')
print(user_info)
for hobby in hobbies:#循环遍历列表元素
if hobby in user_info['hobbies']:#列表内的元素在大明的爱好中
print(hobby.title()+' is in the dictionary of Daming.')
else:
print(hobby.title()+' is not in the dictionary of Daming')
for hobby in user_info['hobbies']: # 循环遍历用户爱好这一栏的列表元素
if hobby in hobbies: # 大明的爱好在列表元素内
print(hobby.title() + ' is in the list of hobbies.')
else:
hobbies.append(hobby)#将大明的爱好添加到列表元素内
print('The modified list of hobbies:')
print(hobbies)
运行效果:
{'Daming': {'age': 12, 'hobbies': ['reading', 'writing', 'drawing'], 'location': 'China'}, 'Lingling': {'age': 22, 'hobbies': ['reading', 'writing', 'watching Tv'], 'location': 'England'}}
the original list of hobbies:
['reading', 'writing', 'watching Tv', 'playing football']
It is the infomation of Daming:
{'age': 12, 'hobbies': ['reading', 'writing', 'drawing'], 'location': 'China'}
Reading is in the dictionary of Daming.
Writing is in the dictionary of Daming.
Watching Tv is not in the dictionary of Daming
Playing Football is not in the dictionary of Daming
Reading is in the list of hobbies.
Writing is in the list of hobbies.
The modified list of hobbies:
['reading', 'writing', 'watching Tv', 'playing football', 'drawing']
Process finished with exit code 0
4.动手试一试:
6-7:
#定义3个字典
user1={'name':'huahua','age':12}
user2={'name':'lingling','age':22}
user3={'name':'hututu','age':13}
#字典列表
user=[user1,user2,user3]
print('字典列表输出:')
print(user)
for user_info in user[:]:#遍历列表,user_info是字典类型
for key,value in user_info.items():#遍历字典
if value=='huahua':
print('The age of {} is '.format(value)+str(user_info['age'] ))#利用format()函数获取大括号里面的内容
字典列表输出:
[{'name': 'huahua', 'age': 12}, {'name': 'lingling', 'age': 22}, {'name': 'hututu', 'age': 13}]
The age of huahua is 12
6-8:
pets=[]
doudou={'type':'哈士奇','person':'小明'}
tomato={'type':'柯基','person':'小花'}
potato={'type':'边牧','person':'湘湘'}
pets=[doudou,tomato,potato]
print('输出字典列表:')
print(pets)
#遍历字典列表,并将宠物的所有信息都输出
for dog_name in pets:#遍历列表
for key in dog_name:#遍历字典里面的键
if key=='type':
print('品种:'+dog_name['type'])
if key=='person':
print('主人:' + dog_name['person'])
输出字典列表:
[{'type': '哈士奇', 'person': '小明'}, {'type': '柯基', 'person': '小花'}, {'type': '边牧', 'person': '湘湘'}]
品种:哈士奇
主人:小明
品种:柯基
主人:小花
品种:边牧
主人:湘湘
6-12:
milktea_info={
'1':{
'milktea_name':'红豆相思牛奶茶',
'price':[12,13,14],
'milktea_size':['小杯','中杯','大杯'],
},
'2':{
'milktea_name':'草莓牛奶茶',
'price':[12,14,16],
'milktea_size':['小杯','中杯','大杯'],
},
'3':{
'milktea_name':'香蕉牛奶茶',
'price':[12,13,15],
'milktea_size':['小杯','中杯','大杯'],
}
}
print('输出奶茶信息:')
print(milktea_info)
milktea_number=input('请输入您想要的奶茶编号:')
for info,value in milktea_info.items():#value存放的是字典信息
if info=='1' and milktea_number=='1':
print('1号奶茶信息:')
print(milktea_info[info])
for name in value:#遍历字典的键,遍历3次
if name=='price' or name=='milktea_size':
print(value[name])
for type in value[name]:#遍历列表price和milktea_size
print(type)
输出奶茶信息:
{'1': {'milktea_name': '红豆相思牛奶茶', 'price': [12, 13, 14], 'milktea_size': ['小杯', '中杯', '大杯']}, '2': {'milktea_name': '草莓牛奶茶', 'price': [12, 14, 16], 'milktea_size': ['小杯', '中杯', '大杯']}, '3': {'milktea_name': '香蕉牛奶茶', 'price': [12, 13, 15], 'milktea_size': ['小杯', '中杯', '大杯']}}
请输入您想要的奶茶编号:2
[12, 13, 14]
12
13
14
['小杯', '中杯', '大杯']
小杯
中杯
大杯
[12, 14, 16]
12
14
16
['小杯', '中杯', '大杯']
小杯
中杯
大杯
[12, 13, 15]
12
13
15
['小杯', '中杯', '大杯']
小杯
中杯
大杯
更多推荐
已为社区贡献3条内容
所有评论(0)