#coding:utf8 people={"baidu":{"apple":10,"orange":20}, "sohu":{"banner":5,"apple":13}, "youku":{"orange":3,"cup":4}, "qq":{"pies":3,"apple":90} } def getNumber(website,item): numBrought=0 for k,v in website.items(): numBrought = numBrought+v.get(item,0) return numBrought # 打印出水果种类 def printBroughtList(website): brountList=[] for k in website.values(): #在进行一次循环判断 for v in k: if(v not in brountList): brountList+=[v] return brountList myList = printBroughtList(people) print "展示所有水果种类:" print myList yourFruit = raw_input() if yourFruit not in myList: print "没有此类水果" else: print "有"+ yourFruit+ " : "+str(getNumber(people,yourFruit))+"个!"
0则评论给“简单测试python字典”