关于python字典中的fromkeys和append添加值及字典嵌套
#!usr/bin/env python #-*- coding=utf-8 -*- #出现问题 为什么enumerate之后 用fromkeys 查询4,6不显示 # li = ['11','22'] # a = dict(enumerate(li,5)) # print a # # b = a.fromkeys(['22','33']) # print b #直接弄一个字典,fromkeys可以创建新字典 # li = {'1':'k1','2':'k2'} # a = li.fromkeys(['1','3'],'alex') # print a #赋值(当给2赋值时 如果不是用中括号来表示的话是不能进行赋值的 即 字符串(str)不能用append添加! # deq = {'1':'alo','2':[]} # deq['2'].append(2) # print deq #看看fromkeys deq = dict.fromkeys(['1','2','3'],['mjb']) print deq deq['2'].append('m') print deq
for i in deq:
print i
结果:
{'1': ['mjb'], '3': ['mjb'], '2': ['mjb']}
{'1': ['mjb', 'm'], '3': ['mjb', 'm'], '2': ['mjb', 'm']}
1
3
2
进程已结束,退出代码0
然后学习了一个for输出 字典 进行省市查询
1 S_deq = { 2 '吉林':{"长春":['百脑汇','火锅'], 3 '延吉':['酒馆','网吧'] 4 }, 5 '山东':{ 6 '济南':['蓝翔','挖掘机'], 7 '济宁':['饭店','按摩院','电影院'] 8 } 9 }#进行字典的定义 10 for i in S_deq:#进入第一层 11 print (i) 12 Q1 = raw_input ("请输入您要查询的") 13 Q2 = S_deq[Q1] 14 for i2 in Q2:#进入第二层 15 print (i2) 16 for i3 in Q2[raw_input("请输入您要查询的")]:#进入第三层 17 print (i3)
用至今为止的知识做了一个简单的小购物车
1 all_Money = int(raw_input("请输入您的金钱总额:")) 2 goods =[ 3 {"1":'电脑','price':1000}, 4 {"2":'手机','price':500}, 5 {"3":'平板电脑','price':250} 6 ] 7 for buy_Goods in goods: 8 print (buy_Goods) 9 10 buy_What = int(raw_input("您要买啥啊?")) 11 12 while True: 13 if goods[buy_What-1]['price'] < all_Money: 14 print ("购买成功!") 15 break 16 elif goods[buy_What-1]['price'] > all_Money: 17 print("余额不足") 18 chongZhi = int(raw_input("请问您是否要充值")) 19 if chongZhi == 1: 20 money_ChongZhi = int(raw_input('请输入您要充值的金钱数额')) 21 all_Money = all_Money + money_ChongZhi 22 else: 23 print ("没钱滚蛋!") 24 break
今天上午为止就这些了 下午再更!2017/12/11 11:07