列表和字典比较

时间:2020-10-17 16:05:57

标签: python-3.x

我有一个列表和一个包含特定项目的字典。如果该项目存在于字典中,并且也在列表中,则在字典中增加该项目的键值:

如果字典中不存在未知项目,我将尝试将其打印出来:

def take_inventory() :
    items = ['shoes','hats','shoes', 'hats','coats','gloves','dresses',
             'shoes', 'hats', 'hats','rings'] 

    emptInvtentory = {'shoes': 0, 'coats':0, 'dresses':0, 'hats':0 }
 
    for key in emptInvtentory:
        for i in items:
            if i == key:
                emptInvtentory[key] +=1
 
    for key, value in emptInvtentory.items():
        print("current stock of {0} is {1}".format(key,value) )



if __name__ == "__main__":
    take_inventory()

我的打印输出:

 current stock of shoes is 3
 current stock of coats is 1
 current stock of dresses is 1
 current stock of hats is 4

我还需要打印“未知物品:手套”

2 个答案:

答案 0 :(得分:2)

我建议您重新检查“字典中”的内容,并修复一些拼写错误:

def take_inventory() :
    items = ['shoes','hats','shoes', 'hats','coats','gloves','dresses',
             'shoes', 'hats', 'hats','rings'] 

    emptyInventory = {'shoes': 0, 'coats':0, 'dresses':0, 'hats':0 }
    unknown = set()
    for value in items:
        if value in emptyInventory:
            emptyInventory[value] +=1
        else:
            # adding it to a new set so the output is orderly
            # first all known things, then all unknown ones
            # if you want to mix and match, simply print item here
            # but you might then get multiple outputs for the same
            # unknown thing
            unknown.add(value)

    for key, value in emptyInventory.items():
        print("current stock of {0} is {1}".format(key,value) )

    for thing in unknown:
        print("Unknown item: ", thing)
     

if __name__ == "__main__":
    take_inventory()

输出:

current stock of shoes is 3
current stock of coats is 1
current stock of dresses is 1
current stock of hats is 4
Unknown item:  gloves
Unknown item:  rings

如果您喜欢冒险,请使用collections.Counter为您计算事物:

from collections import Counter

def take_inventory(items) : 
    allowed_things = {'shoes', 'coats', 'dresses', 'hats'}
    c = Counter(items)

    for allowed in allowed_things:
        print("current stock of {0} is {1}".format(allowed,c[allowed]))

    for thing in c:
        if thing not in allowed_things:
            print("Unknown item: {0} occured {1}".format(thing, c[thing]))

if __name__ == "__main__":
    items = ['shoes','hats','shoes', 'hats','coats','gloves','dresses',
             'shoes', 'hats', 'hats','rings']

    take_inventory(items)

输出:1

current stock of hats is 4 
current stock of coats is 1
current stock of shoes is 3
current stock of dresses is 1
Unknown item: gloves occured 1
Unknown item: rings occured 1

答案 1 :(得分:0)

简单

items = ['shoes','hats','shoes', 'hats','coats','gloves','dresses', 'shoes', 'hats', 'hats','rings']
emptInvtentory = {'shoes': 0, 'coats':0, 'dresses':0, 'hats':0 }
for value in items:
  if value in emptInvtentory:
    emptInvtentory[value] +=1
  else:
    print("Unknown item: ", value)  
for key, value in emptInvtentory.items():
  print("current stock of {0} is {1}".format(key,value) )
相关问题