2015-09-27 100 views
0

我无法弄清楚如何将某些值添加到列表中的每个单独的密钥。我有几个类型(b,m,t,d,c),它们是键,然后我想将这些项目的成本添加到每次通过循环时都是字典值的列表中。 这是我到目前为止有:Python:添加一个项目列表,这是一个字典中的值列表

a={} 
allitemcostb=[] 
allitemcostm=[] 
allitemcostt=[] 
allitemcostd=[] 
allitemcostc=[] 
n=4 

while n>0: 
    itemtype=raw_input("enter the item type-b,m,t,d,c:") 
    itemcost=input("enter the item cost:") 
    if itemtype="b":  
     allitemcostb.append(itemcost) 
     a[itemtype]=allitemcostb 
    if itemtype="m": 
     allitemcostm.append(itemcost) 
     a[itemtype]=allitemcostm 
    if itemtype="t": 
     allitemcostt.append(itemcost) 
     a[itemtype]=allitemcostt 
    if itemtype="d": 
     allitemcostd.append(itemcost) 
     a[itemtype]=allitemcostd 
    if itemtype="c": 
     allitemcostc.append(itemcost) 
     a[itemtype]=allitemcostc 
    else: 
     print "Sorry please enter a valid type" 
    n=n-1 
print a 

它不断给我的错误信息,无论是东西没有定义,或者语法不正确。 感谢

+0

我不知道关于蟒蛇,但你有看到过吗? https://docs.python.org/2/tutorial/datastructures.html#dictionaries – artsylar

回答

2

相反的a[itemtype] = allitemcostb,它只是设置一个键的值到一个新的成本,你需要创建一个list如果该键不存在,或者如果它不添加到现有list。用setdefault()方法做到这一点。

以下用途只是一个itemtype:[itemcost, itemcost...]字典和没有单独list S,省去了手动递增while环赞成for环的与xrange,并用更直接的结构取代,而不是大的分支结构( “如果它是a,做a,”它确实“做任何事情”)。行if itemtype in ('b', 'm', 't', 'd', 'c'):检查输入的itemtype是表示可用选项的单字符字符串。如果输入的itemcost无法转换为float,则会捕获错误,并提示用户再次尝试。

a={} 
n=4 

for i in xrange(n): 
    itemtype = raw_input("enter the item type-b,m,t,d,c:") 
    itemcost = raw_input("enter the item cost:") 
    try: 
     itemcost = float(itemcost) 
    except ValueError: 
     print "Sorry, please enter a valid cost." 
     break 
    if itemtype in ('b', 'm', 't', 'd', 'c'): 
     a.setdefault(itemtype, []).append(itemcost) 
    else: 
     print "Sorry, please enter a valid type." 

print a 
0

试试这个:

a = {} 
all_item_cost_b=[] 
all_item_cost_m=[] 
all_item_cost_t=[] 
all_item_cost_d=[] 
all_item_cost_c=[] 
n = 4 

while n > 0: 
    item_type = input("enter the item type-b,m,t,d,c:") 
    item_cost = input("enter the item cost:") 
    if item_type == "b": 
     all_item_cost_b.append(item_cost) 
     a[item_type] = all_item_cost_b 
    elif item_type == "m": 
     all_item_cost_m.append(item_cost) 
     a[item_type] = all_item_cost_m 
    elif item_type == "t": 
     all_item_cost_t.append(item_cost) 
     a[item_type] = all_item_cost_t 
    elif item_type == "d": 
     all_item_cost_d.append(item_cost) 
     a[item_type] = all_item_cost_d 
    elif item_type == "c": 
     all_item_cost_c.append(item_cost) 
     a[item_type] = all_item_cost_c 
    else: 
     print("Sorry please enter a valid type") 
    n = n - 1 
print(a) 

给我们一个反馈。如果这能解决您的问题,请不要忘记标记为已回答。 干杯。

+0

我想@ TigerHawkT3有一个更专业和整洁的回应。 –

+0

将赋值更改为相等性检查是一个很好的改变,但代码的算法,结果或可读性没有其他改进。另外,'raw_input()'中的'raw_'出于某种原因--OP使用的是Python 2,所以你的'item_type = input(“输入项目类型-b,m,t,d,c: “)'会导致OP的新错误。 – TigerhawkT3

+0

@ TigerhawkT3:“另外,'raw_input()'中的'raw_'出于某种原因......”我也怀疑,因为一旦我将他的代码粘贴到我的编辑器中,那给了我一个错误。我实际上使用python 3.5,从来没有尝过旧版本。谢谢你,对不起OP做更多的伤害。 –

0

以下是两种解决方案。

第一个不是那么严格。这将允许用户输入任何价值的项目类型,但不能为itemcost

a={} 
n=4 

while (n>0): 
    itemtype = input("enter the item type-b,m,t,d,c:") 
    itemcost = input("enter the item cost:") 

    while(True): 
     try: 
      itemcost = float(itemcost) 
      break; 
     except ValueError: 
      print ("Sorry, please enter a valid cost.") 
      itemcost = input("enter the item cost:") 

    if itemtype.lower() in "b m t d c".split(): 
     a[itemtype] = a.get(itemtype,list())+[itemcost] 

    n-=1 

print (a) 

第二种形式将严格既为用户的输入,并会不断提示,直到用户输入的预期值

a={} 
n=4 

while (n>0): 
    itemtype = input("enter the item type-b,m,t,d,c:") 
    ##user enters a wrong value 
    while(itemtype.lower() not in "b m t d c".split()): 
     print ("Sorry, please enter a valid item.") 
     itemtype = input("enter the item type-b,m,t,d,c:") 

    itemcost = input("enter the item cost:") 
    ##user enters a wrong value 
    while(True): 
     try: 
      itemcost = float(itemcost) 
      break; 
     except ValueError: 
      print ("Sorry, please enter a valid cost.") 
      itemcost = input("enter the item cost:") 

    a[itemtype] = a.get(itemtype,list())+[itemcost] 

    n-=1 

print (a) 
+0

如果'input(“输入项目type-b,m,t,d,c:”)'命令不适用于您,请将其更改为'raw_input(“”)'。该命令不适用于我 – rydan

相关问题