从两个列表生成的字典没有做我想做的事情

2024-04-18 03:02:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题是,当给定任何输入(正确格式化)时,字典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)

Tags: ofthe代码inforinput字典bar
3条回答

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

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}}

您的代码有一些问题,但我将只解决这些问题以使您的用例正常工作:

  1. 每次都会覆盖舞者的海报:
    舞者位置。更新({dancer:{}})

那没必要。这应该是一个缩进水平

  1. 您当前正在创建一个元组,它是不可变的。你需要使用一个列表

试试这个:

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}]}

这似乎是你想要的答案

相关问题 更多 >