本文转载:https://xiaochuhe.blog.csdn.net/article/details/122530524
一、知识点21~25
#21.列表list取值嵌套
a = [['1','2'],['3','4']]
print (a)
print (a[0])
print (a[0][0])#第一个列表内取值第一个值
#22.练习:八个对象随机分配到三个库,应用random库
import random
offices =[[],[],[]]
names = ["a","b","c","d","e","f","g","d"]
for name in names:
a = random.randint(0,2)
offices[a].append(name)#i = 1
for office in offices:
print("分配个数为%d"%(len(office)),"-"*20) #i += 1
for name in office:
print("对象名字:%s"%name)
#print("\n")
print("-"*20)
#23.元组tuple的增删改查
a = (1,2,3,'青鸟')
b = (4,)
a = a + b #增
print (a)
# del a 删除整个元组
#24.元组的转换
a = [1,2]
a = tuple()
print (a,type(a))
#25.字典知识点:dict 存储形式(key,value)键值对
dict = {'name':'xiaochuhe','age':'21'}
print (dict['name'])#防止访问对象不存在print(dict.get("age"))
print(dict.get("a","22")) #未找到设定默认值
输出结果:
二、知识点26~30
#26.字典的修改
dict = {'name':'xiaochuhe','age':'21'}
add = input("新的信息是:")
dict['name'] = add
print (dict['name'],dict)
#27.字典的删除
dict = {'name':'xiaochuhe','age':'21'}
print("删除前:{name}:{age}".format(**dict))
del dict['name']#删除一个键值
print (dict)
dict.clear()#清空字典内容
print (dict)
#28.字典的增加
dict = {'name':'xiaochuhe','age':'21'}
dict['hobby']='learning'
print (dict)
#29.字典的查
dict = {'name':'xiaochuhe','age':'21'}
print (dict.keys())#得到所有的键
print (dict.values())#得到所有的值
print (dict.items())#得到所有的键值对
#30.遍历字典所有的键值对
dict = {'name':'xiaochuhe','age':'21'}
for keys in dict.keys():#遍历键
print (keys,end='')
print ('\n---------------------------')
for keys in dict.values():#遍历值
print (keys,end='')
print ('\n---------------------------')
for keys in dict.items():#遍历键值对
print (keys,end='')
print ('\n---------------------------')
输出结果: