字典
增:xxx [新的关键] =值
删除:删除xxx [键]
改:xxx[已存在的key] = new值
查:xxx.get(key)
列表:[“1”,2,3.14]
字典:{“name”:“名字”,“age”:12}
下面是例子
#-- coding:utf-8 --
#信息列表
infor = [{“name”:“沈政委”,“sex”:“男”,“age”:22},{“name”:“杨丽云”,“sex”:“女”,“age”:20},{“name”:“周玲”,“sex”:“女”,“age”:20}]
#功能提示
print("="*50)
print(“信息管理系统”)
print(“1.添加一个新的信息”)
print(“2.删除一个人的信息”)
print(“3.修改一个人的信息”)
print(“4.查找一个人的信息”)
print(“5.退出”)
print("="*50)
while True:
#获取用户输入
num = int(input(“请输入需要操作的序号:”))
#更具用户输入执行相应操作
if num == 1:
add_name = input("请输入你要添加的名字:")
add_sex= input("请输入你要添加的性别:")
add_age = input("请输入你要添加的年龄:")
add_infor = {}
add_infor['name'] = add_name
add_infor['sex'] = add_sex
add_infor['age'] = add_age
append_insert = input("向后追加Y,指定插入N:")
if append_insert =="Y" or append_insert == "y":
infor.append(add_infor)
elif append_insert == "N" or append_insert =="n":
insert_num = int(input("请输入指定插入的下标:"))
infor.insert(insert_num,add_infor)
else:
print("恁输入的选项不正确!!")
print(infor)
elif num==2:
print(infor)
choose_one = input("pop-->删除最后一个 remove-->删除信息 del-->按下标删除")
if choose_one == "pop":
infor.pop()
print(infor)
elif choose_one == "remove":
del_name = input("请输入你要删除的信息:")
infor.remove(del_name)
print(infor)
elif choose_one == "del":
del_infor_num = int(input("请输入你要删除某人信息对应下标:"))
print(infor[del_infor_num])
key_del = input("删除该人的整个信息Y,删除该人的某个信息N:")
if key_del =="Y" or key_del =="y":
del infor[del_infor_num]
elif key_del == "N" or key_del == "n":
del_infor_key = input("请输入你要删除的某个信息")
del infor[del_infor_key]
else:
print("输入有误")
print(infor)
else:
print("请输入pop remove del...")
elif num ==3:
print(infor)
amend_infor_num = int(input("请输入你要修改的信息的下标:"))
print(infor[amend_infor_num])
amend_details=infor[amend_infor_num] #新建一个修改的字典
amend_del = input("修改该同学的某条信息Y,删除该同学的某条信息N:")
if amend_del =="Y" or amend_del =="y":
amend_key = input("请输入你要修改的key:")
amend_infor = input("请输入你修改后的信息:")
amend_details[amend_key] = amend_infor
print(infor[amend_infor_num])
elif amend_del == "N" or amend_del == "n":
amend_key_del = input("请输入你要删除的key:")
del amend_details[amend_key_del]
print(infor[amend_infor_num])
#infor[amend_infor_num] = amend_infor
print(infor)
elif num == 4:
print(infor)
seek_name = input("请输入你要查找的信息:")
#if seek_name in infor:
#print("找到了")
for information in infor:
new_inform = information
print(new_inform.get(seek_name))
elif num == 5:
break;
else:
print("您的输入有误!请重新输入!")
作者:谢桥
来源:CSDN
原文:https://blog.csdn.net/title71/article/details/80464427
版权声明:本文为博主原创文章,转载请附上博文链接!