[英]How to compare values of two dictionaries that have different keys in python
我的足球比赛结果字典看起来像:
results = {
"31 August 2019": [
[
{"Lazio": 2},
{"Roma": 1}
]
],
"1 September 2019": [
[
{"AC Milan": 0},
{"Napoli": 3}
],
[
{"Udinese": 1},
{"Fiorentina": 1}
]
]
}
[{"Lazio": 2},{"Roma": 1}] 表示拉齐奥VS罗马,比分:2-1
我需要的是通过比较两个字典的比赛来获得每场比赛的获胜者。 我试过这个:
for date, matches in results.items():
for match in matches:
if match[0].value() > match[1].value(): #I'm Stuck on this
print(date)
print("the winner is :", match[0].key())
elif match[0].value() < match[1].value(): #I'm Stuck on this
print(date)
print("the winner is :", match[1].key())
else:
print(date)
print("the match between ",match[0].key()," VS ",match[1].key()," was even")
当然,该代码不起作用,但我希望您能理解我的意思。 如果您有更好的格式来保存结果,请告诉我。 非常感谢
与简单地使用 2-tuple ('AC Milan', 0)
类的东西相比,您的字典{'AC Milan': 0}
不是一种方便的格式。
以下是关于如何提取数据的一个建议:
results = {'1 September 2019': [[{'AC Milan': 0}, {'Napoli': 3}],
[{'Udinese': 1}, {'Fiorentina': 1}]],
'31 August 2019': [[{'Lazio': 2}, {'Roma': 1}]]}
for date, matches in results.items():
for match in matches:
t1, t2 = [next(iter(d.items())) for d in match]
team1, score1 = t1
team2, score2 = t2
if score1 > score2:
print(team1)
elif score2 > score1:
print(team2)
else:
print("the match between {} VS {} was even"
.format(team1, team2))
给出:
Napoli
the match between Udinese VS Fiorentina was even
Lazio
将它们更改为 2 元组,这变得容易得多。 例如:
results = {'1 September 2019': [[('AC Milan', 0), ('Napoli', 3)],
[('Udinese', 1), ('Fiorentina', 1)]],
'31 August 2019': [[('Lazio', 2), ('Roma', 1)]]}
for date, matches in results.items():
for match in matches:
t1, t2 = match
team1, score1 = t1
team2, score2 = t2
# ... etc ...
(或者甚至只是for t1, t2 in matches:
代替for match...
和以下语句。)
您的代码中的主要问题是字典没有.value()
方法,即使它们只有一个键值对。 对于提供的数据结构,您需要改为执行类似的操作,通过list(dict.values())[0]
从每个单个元素字典中获取值(请注意,这是一种替代方法(并且速度较慢)在其他答案中看到的next(iter(d.items()))
):
for date,matches in results.items():
for match in matches:
if list(match[0].values())[0] > list(match[1].values())[0]:
print(date)
print("the winner is :", list(match[0].keys())[0])
elif list(match[0].values())[0] < list(match[1].values())[0]:
print(date)
print("the winner is :", list(match[1].keys())[0])
else:
print(date)
print("the match between ",list(match[0].keys())[0],
" VS ",list(match[1].keys())[0]," was even")
但我同意其他发帖者的观点,即可以改进数据结构以减少这种感觉。
我对转换的建议是为每个日期提供一个 2 元素字典列表(仍然键 = 团队和值 = 分数)。 然后你可以做这样的事情,感觉更简单:
results = {"31 August 2019": [{"Lazio": 2, "Roma": 1}], "1 September 2019" : [{"AC Milan":0,"Napoli":3},{"Udinese":1,"Fiorentina":1}]}
for date,matches in results.items():
for match in matches:
print(date)
maxi = max(match, key=match.get)
mini = min(match, key=match.get)
if match[maxi] == match[mini]:
print("the match between ",maxi," VS ",mini," was even")
else:
print('the winner is: ', maxi)
match[0]
和match[1]
这里都是字典,没有值方法。 字典可以有多个键和值,因此使用“值”方法来检索一个值是没有任何意义的。
相反,它具有.values()
方法,该方法返回一个可用于遍历所有值的可迭代对象。 如果您确定字典将始终包含一个且仅一个值,则可以使用next(iter(match[0].values()))
检索它。 如果有多个值,这将只检索一个值,如果 dict 为空,则会抛出异常。 因此,您的比较看起来像if next(iter(match[0].values())) > next(iter(match[1].values())):
但是,如果您可以控制results
的结构,我会避免这种情况。 使用 dict 存储一个键值对是不合适的。 改为使用元组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.