我的 Ordered Dict 没有按预期工作
My Ordered Dict is not working as I expected
基本上我有这个代码:
from collections import OrderedDict as OD
person = OD({})
for num in range(10):
person[num] = float(input())
tall = max(person.values())
short = min(person.values())
key_tall = max(person.keys())
key_short = min(person.keys())
print(f'The shortest person is the person number {key_short} who is {short}meters tall')
print(f'The tallest person is the person number {key_tall} who is {tall}meters tall')
理论上,当我在字典中输入 10 个人时,第一个数字为 1,一直到 9,最后一个为 0,输出应该是:
The shortest person is the person number 9 who is 0.0m meters tall
The tallest person is the person number 8 who is 9.0m meters tall
但实际上它打印:
The shortest person is the person number 0 who is 0.0m meters tall
The tallest person is the person number 9 who is 9.0m meters tall
出于某种原因,当我的字典的值从 1 一直到 10 时,它工作正常。
关于为什么会发生这种情况以及如何解决它有什么想法吗?
key_tall = max(person.keys())
key_short = min(person.keys())
你的 keys 是整数 0..9
所以对于这两个值你会得到 9
和 0
,因为您要求 min/max 键而不考虑值。
您 似乎 在具有 highest/lowest 值的人的密钥之后,但这不是该代码会给您的。
如果您在寻找具有最大值的项目的索引,您可以执行以下操作:
indexes_tall = [idx for idx in range(len(person)) if person[idx] == max(person.keys())]
这将为您提供匹配最高值的索引列表,然后您可以根据需要进行处理。举个例子:
from collections import OrderedDict as OD
person = OD({})
for num in range(10):
person[num] = float((num + 1) % 10) # effectively your input
tall = max(person.values())
short = min(person.values())
keys_tall = [str(idx + 1) for idx in range(len(person)) if person[idx] == max(person.keys())]
keys_short = [str(idx + 1) for idx in range(len(person)) if person[idx] == min(person.keys())]
print(f'The shortest height of {short}m is held by: {" ".join(keys_short)}')
print(f'The tallest height of {tall}m is held by: {" ".join(keys_tall)}')
会给你:
The shortest height of 0.0m is held by: 10
The tallest height of 9.0m is held by: 9
基本上我有这个代码:
from collections import OrderedDict as OD
person = OD({})
for num in range(10):
person[num] = float(input())
tall = max(person.values())
short = min(person.values())
key_tall = max(person.keys())
key_short = min(person.keys())
print(f'The shortest person is the person number {key_short} who is {short}meters tall')
print(f'The tallest person is the person number {key_tall} who is {tall}meters tall')
理论上,当我在字典中输入 10 个人时,第一个数字为 1,一直到 9,最后一个为 0,输出应该是:
The shortest person is the person number 9 who is 0.0m meters tall
The tallest person is the person number 8 who is 9.0m meters tall
但实际上它打印:
The shortest person is the person number 0 who is 0.0m meters tall
The tallest person is the person number 9 who is 9.0m meters tall
出于某种原因,当我的字典的值从 1 一直到 10 时,它工作正常。
关于为什么会发生这种情况以及如何解决它有什么想法吗?
key_tall = max(person.keys())
key_short = min(person.keys())
你的 keys 是整数 0..9
所以对于这两个值你会得到 9
和 0
,因为您要求 min/max 键而不考虑值。
您 似乎 在具有 highest/lowest 值的人的密钥之后,但这不是该代码会给您的。
如果您在寻找具有最大值的项目的索引,您可以执行以下操作:
indexes_tall = [idx for idx in range(len(person)) if person[idx] == max(person.keys())]
这将为您提供匹配最高值的索引列表,然后您可以根据需要进行处理。举个例子:
from collections import OrderedDict as OD
person = OD({})
for num in range(10):
person[num] = float((num + 1) % 10) # effectively your input
tall = max(person.values())
short = min(person.values())
keys_tall = [str(idx + 1) for idx in range(len(person)) if person[idx] == max(person.keys())]
keys_short = [str(idx + 1) for idx in range(len(person)) if person[idx] == min(person.keys())]
print(f'The shortest height of {short}m is held by: {" ".join(keys_short)}')
print(f'The tallest height of {tall}m is held by: {" ".join(keys_tall)}')
会给你:
The shortest height of 0.0m is held by: 10
The tallest height of 9.0m is held by: 9