列表:有序,元素可以被修改
元组:
dict:
info = {
1:'asdf',
"k1":'wqwr',
True:"123",
(11,22):123
"kk3":(11.22)
}
dict.fromkeys(["k1",123,"999"],123)
.setdefault 设置值,若已存在,不设置,获取当前key对应的值;不存在,设置值,获取当前key对应的值
字典的查找:
#!/usr/bin/env python
# -*- coding=utf-8 -*-
dic = {
u"植物":
{u"草本植物":
[u"荷花", u"叶子", u"小草", u"菊花", u"兰花", u"茉莉", u"葫芦"],
u"木本植物":
[u"腊梅",u"梅花",u"玉兰花",u"迎春花",u"榆叶梅",u"杨树",u"苹果"],
u"水生植物":
[u"荷花",u"芦苇",u"香蒲",u"菰",u"水葱",u"芦竹",u"菖蒲",u"蒲苇"]
},
u"动物":
{u"家禽":
[u"小猫",u"小狗",u"小猪",u"蛇",u"小鸟",u"鱼",u"老虎",u"狮子"],
u"两栖动物":
[u"雨蛙",u"树蛙",u"蟾蜍",u"大鲵",u"蝾螈",u"蚓螈",u"吻蚓",u"鱼螈"],
u"哺乳类动物":
[u"貘树懒",u"斑马",u"狼",u"虎",u"老鼠",u"麋鹿",u"猴子",u"猞猁",u"穿山甲"]
}
}
li = []
bi = []
go = True
while go:
for i,v in enumerate(dic,1):
print i,v
li.append(v)
scan_1 = input(">>>")
while go:
for i,v in enumerate(dic[li[scan_1 - 1]],1):
print i,v
bi.append(v)
scan_2 = raw_input(">>>")
if scan_2 == "b":
break
elif scan_2 == "q":
go = False
break
elif scan_2 == "1" or scan_2 == "2" or scan_2 == "3":
while go:
for i, v in enumerate(dic[li[scan_1 - 1]][bi[int(scan_2) - 1]], 1):
print i, v
scan_3 = raw_input(">>>>>")
scan_3 = str(scan_3)
scan_3 = scan_3.lower()
if scan_3 == "b":
break
elif scan_3 == "q":
go = False
break
else:
print(u"输入有误,请重新输入")
else:
print(u"输入有误,请重新输入")
#!/usr/bin/env python
# -*- coding=utf-8 -*-
db = {
"广州":{},
"北京":{
"昌平":{
"沙河":{},
"回龙观":{}
},
"朝阳":{},
"海淀":{}
},
"上海":{}
}
path = []
while True:
temp = db
for item in path:
temp = temp[item]
print("当前可选的所有子节点:",list(temp.keys()))
choice = input('1: 添加节点; 2: 查看节点(Q/B);\n >>>')
if choice == '1':
name = input("请输入要添加的节点名称:")
temp[name] = {}
elif choice == '2':
name = input("请输入要查看的节点名称:")
path.append(name)
elif choice.lower() == 'b':
path.pop()
elif choice.lower() == 'q':
break
转载于:https://blog.51cto.com/14052013/2314472