从2个列表生成的字典没有做我想要的

时间:2016-06-16 02:22:54

标签: python dictionary user-input

我的问题是,当给出任何输入(格式正确)时,根据我的代码没有正确生成字典dancer_placings。输入1个舞者,1号,2个舞蹈,名为foo和bar,字典dancer_placings{'1': {'bar': 0}},当我希望它为{'1': {'foo': 0},{'bar': 0}}

我显然犯了一个错误,那么如何修复我的代码以便它能够完成我打算做的事情呢?

这是我的代码:

print("Welcome to Highland Scrutineer")

dancers = []
dances = []
dancer_placings = {}

dancers = []
dancer_num = int(input("How many dancers were there?: "))
while len(dancers) + 1 <= dancer_num:
    dancers.append(input("What was the number of the dancer? "))

print("The list of dancers is:")
for dancer in dancers:
    print(dancer)

dances = []
dance_num = int(input("How many dances did these dancers compete in? "))

while len(dances) + 1 <= dance_num:
    dances.append(input("What was the name of the dance? "))

print("The list of dances is:")
for dance in dances:
    print(dance)

for dancer in dancers:
    for dance in dances:
        dancer_placings.update({dancer:{}})
        dancer_placings[dancer].update({dance:0})

print(dancer_placings)

3 个答案:

答案 0 :(得分:0)

我认为问题是你要为每个舞蹈覆盖dancer_placings的值。小调整应该返回您正在寻找的结果:

for dancer in dancers:
    dancer_placings.update({dancer: {}})
for dance in dances:
    dancer_placings[dancer].update({dance: 0})

我通过快速划痕测试运行它,这就是结果:

{'1': {'bar': 0, 'foo': 0}}

答案 1 :(得分:0)

您的代码存在一些问题,但我只是解决这些问题以使您的用例有效:

  1. 你每次都会覆盖舞者的位置:
    dancer_placings.update({舞者:{}})
  2. 没有必要。这应该是一个缩进级别。

    1. 您正在创建一个不可变的元组。您需要使用列表。
    2. 试试这个:

      print("Welcome to Highland Scrutineer")
      
      dancers = []
      dances = []
      dancer_placings = {}
      
      dancers = []
      dancer_num = int(input("How many dancers were there?: "))
      while len(dancers) + 1 <= dancer_num:
          dancers.append(input("What was the number of the dancer? "))
      
      print("The list of dancers is:")
      for dancer in dancers:
          print(dancer)
      
      dances = []
      dance_num = int(input("How many dances did these dancers compete in? "))
      
      while len(dances) + 1 <= dance_num:
          dances.append(input("What was the name of the dance? "))
      
      print("The list of dances is:")
      for dance in dances:
          print(dance)
      
      for dancer in dancers:
          dancer_placings[dancer] = []
          for dance in dances:
              dancer_placings[dancer].append({dance:0})
      
      print(dancer_placings)
      

      因此,这导致以下输出:

      user-macbookpro2:~ user$ python test.py
      
      Welcome to Highland Scrutineer
      
      How many dancers were there?: 1
      
      What was the number of the dancer? 1
      
      The list of dancers is:
      
      1
      
      How many dances did these dancers compete in? 2
      
      What was the name of the dance? 'foo'
      
      What was the name of the dance? 'bar'
      
      The list of dances is:
      
      foo
      
      bar
      
      {1: [{'foo': 0}, {'bar': 0}]}
      

      这似乎是你想要的答案!

答案 2 :(得分:0)

这里讨论的代码是:

for dancer in dancers:
    for dance in dances:
        dancer_placings.update({dancer:{}})
        dancer_placings[dancer].update({dance:0})

正如现在所写,每次通过舞者的迭代都会在每个舞蹈中循环。但是,语句dancer_placings.update({dancer:{}})将在内循环的每次迭代中“清除”当前舞者的值。因此,只有最后一次内部迭代“坚持”。

你想要的是这样的:

for dancer in dancers:
    dancer_placings.update({dancer:{}})
    for dance in dances:    
        dancer_placings[dancer].update({dance:0})

这将为外循环中的每个舞者创建一个空字典,并更新内循环中的舞蹈。

所以这会产生(注意字典键没有明确的顺序):

Welcome to Highland Scrutineer
How many dancers were there?: 3
What was the number of the dancer? 1
What was the number of the dancer? 2
What was the number of the dancer? 3
The list of dancers is:
1
2
3
How many dances did these dancers compete in? 4
What was the name of the dance? a
What was the name of the dance? b
What was the name of the dance? c
What was the name of the dance? d
The list of dances is:
a
b
c
d
{'1': {'a': 0, 'b': 0, 'd': 0, 'c': 0}, '3': {'a': 0, 'b': 0, 'd': 0, 'c': 0}, '2': {'a': 0, 'b': 0, 'd': 0, 'c': 0}}