简体   繁体   English

Python:字典打印值而不是键

[英]Python: Dictionary printing value instead of key

This is my code: 这是我的代码:
The line with if should do: compare first value of Dict1 and Dict2 , then it would print first key of whether dict has smaller value on its first place and then it would delete first key and value from that dictionary. 如果有应该做的线:比较的第一个 Dict1Dict2 ,然后将打印的字典是否在它的第一个地方较小的第一个 ,然后它会删除该字典第一个键和值 Note the while loop at start. 请注意启动时的while循环。

What it does : 它的作用是
1. It prints the values, instead of keys. 1.打印值,而不是键。 2. It prints 1 less time instead of as it should, ie if dict 1 has 2 keys and dict2 has 3 keys it should output 5 lines, but it outputs only 4. Feel free to help yourself with Dict1 = {"Player1": 46, "Player2": 34} Dict2 = {"Player3": 38, "Player4": 55} and it should output *keys** Player2 then Player3 then Player1 then Player4, each in its own line, 2.它打印的时间减少了1分钟,而不是应有的打印时间,即,如果dict 1有2个键,而dict2有3个键,它将输出5行,但仅输出4行。Dict1 = {“ Player1”可以帮助您自己: 46,“ Player2”:34} Dict2 = {“ Player3”:38,“ Player4”:55}并且它应该输出* keys ** Player2然后是Player3然后是Player1然后是Player4,每个都在自己的行中,

 while Dict1 and Dict2:

     if Dict1.get(list(Dict1.keys())[0]) > Dict2.get(list(Dict2.keys())[0]):
         print(Dict2[list(Dict2.keys())[0]])
         del Dict2[list(Dict2.keys())[0]]
     elif Dict1.get(list(Dict1.keys())[0]) < Dict2.get(list(Dict2.keys())[0]):
         print(Dict1[list(Dict1.keys())[0]])
         del Dict1[list(Dict1.keys())[0]]

Edit: I got help from user/Mark Tyler. 编辑:我从用户/马克·泰勒那里得到了帮助。 He told me this code: 他告诉我这段代码:

for (key1, val1), (key2, val2) in zip(Dict1.items(), Dict2.items()):
    if val1 > val2:
      print(key2)
    elif val1 < val2:
      print(key1)

but outputs only once and it should output every key from Dict1 and Dict2 What i must do: This is about football. 但是只输出一次,它应该输出Dict1和Dict2中的每个键。我必须做的是:这是关于足球的。 Dict1's keys are names of players who scored, and its values are in that minute they scored. Dict1的键是得分者的名字,其值在他们得分的那一刻。 All for one team. 全部一支团队。 Dict2 is the same for another team. Dict2对于另一个团队来说是相同的。 I already sorted Dict1 and Dict2 by its values. 我已经按其值对Dict1和Dict2进行了排序。 I need to output players as they scored through the match (player who scored first in the first line, who scored second in second line etc.) 我需要输出他们在比赛中得分的球员(第一线得分第一,第二线得分第二的球员,等等)

How about the code below that merge the to dicts into a sorted list of tuples. 下面的代码如何将to dicts合并为元组的已排序列表。

team1 = {"Player1": 46, "Player2": 34}
team2 = {"Player9": 89, "Player3": 38, "Player4": 55}

both_teams = [(k, v) for k, v in team1.items()]
both_teams.extend([(k, v) for k, v in team2.items()])
sorted_both_teams = sorted(both_teams, key=lambda x: x[1])
for entry in sorted_both_teams:
    print('Player {} scored at {}'.format(entry[0], entry[1]))

Output 输出量

Player Player2 scored at 34
Player Player3 scored at 38
Player Player1 scored at 46
Player Player4 scored at 55
Player Player9 scored at 89

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM