我的问题是,当给出任何输入(正确格式化)时,字典dancer_placings
无法根据我的代码正确生成。1个舞者的输入,有1,2舞,名为foo and 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)
您的代码有一些问题,但我只是解决使您的用例工作的问题:
- 您每次都在覆盖舞蹈置:
dancer_placings.update({{舞者:{}})
这不是必需的。那应该是缩进水平。
- 您目前正在创建一个不变的元组。您需要使用列表。
尝试以下操作:
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}]}
这似乎是您所需的答案!
我认为您的问题是您正在为每种舞蹈的舞者_placings覆盖值。次要调整应返回您要寻找的结果:
for dancer in dancers:
dancer_placings.update({dancer: {}})
for dance in dances:
dancer_placings[dancer].update({dance: 0})
我通过快速的刮擦测试进行了运行,这就是结果:
{'1': {'bar': 0, 'foo': 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}}