简体   繁体   English

如何获取包含字典的列表的值以与另一本字典进行比较?

[英]How do I get values of a list containing dictionary to compare with another dictionary?

Here is my list of dictionary这是我的字典列表

scores = [{'names':'soccer', 'power':'5'},{'names':'football', 'power':6}]

as you can see my I may have strings in power values.正如您所看到的,我的 I 可能在幂值中有字符串。

My dictionary I want to compare with if names of scores value(soccer) is equal to.我的字典我想与分数值(足球)的名称是否相等进行比较。

my_stats = {'soccer':3, 'football':2}

So for example所以例如

for values in scores:
    if my_stats(index) == "soccer" and scores(names) == "soccer":
        if my_stats(value) < power(value):
            return power(value) is higher

I know this code doesn't work, but this is my first time trying to get dictionary values from a list.我知道这段代码不起作用,但这是我第一次尝试从列表中获取字典值。 I am guessing I need 2 for loops for this?我猜我需要 2 个 for 循环? 1 for the list and 1 for the dictionary? 1 代表列表,1 代表字典?

I added a few more entries into the scores list to make the code more generic.我在scores列表中添加了更多条目以使代码更通用。

The below code will scan through the full scores list and find the max value of the unique games in my_stats .下面的代码将扫描完整的scores列表,并在my_stats找到唯一游戏的my_stats

scores = [{'names':'soccer', 'power':'5'},
          {'names':'soccer', 'power':8},
          {'names':'football', 'power':6},
          {'names':'football', 'power':9}]
my_stats = {'soccer':3, 'football':2}

for score in scores:
    for game,power in my_stats.items():
        if score['names'] == game and int(score['power']) > power:
            my_stats[game] = int(score['power'])

print (my_stats)

The output of this will be:输出将是:

{'soccer': 8, 'football': 9}

The output of your original scores list is:原始scores列表的输出是:

{'soccer': 5, 'football': 6}

Expanded code扩展代码

You may want to look at this option as well.您可能还想查看此选项。 If the game in scores does not exist in my_stats , then the below code will also add the game into my_stats如果scores中的游戏在my_stats中不存在,那么下面的代码也会将游戏添加到my_stats

scores = [{'names':'soccer', 'power':'5'},
          {'names':'soccer', 'power':8},
          {'names':'football', 'power':6},
          {'names':'football', 'power':9},
          {'names':'basketball', 'power':20}]
my_stats = {'soccer':3, 'football':2}

for score in scores:
    if score['names'] in my_stats:
        if int(score['power']) > my_stats[score['names']]:
            my_stats[score['names']] = int(score['power'])
    else:
        my_stats[score['names']] = int(score['power'])

    #for game,power in my_stats.items():
    #    if score['names'] == game and int(score['power']) > power:
    #        my_stats[game] = int(score['power'])

print (my_stats)

The above code will give you an output as follows:上面的代码会给你一个输出如下:

{'soccer': 8, 'football': 9, 'basketball': 20}
for score in scores:
   if score['names'] == 'soccer':
      if score['power'] == my_stats['soccer']
        print('the name is soccer and the scores are even')

Let me know if this is what you're trying to do!如果这就是你想要做的,请告诉我! :) :)

Note: in general, if you want to compare dictionaries, I'd advise to keep the dictionary keys the same.注意:一般来说,如果你想比较字典,我建议保持字典键相同。 I'm not sure what your goal is, but for example, I'd change the structure of scores to be:我不确定您的目标是什么,但例如,我会将scores结构更改为:

scores = {'power':
             {'football': 5, 'soccer': 6}
         }

This way you could do:这样你可以这样做:

if scores['powers']['football'] == my_stats['football']:
    print('scores are the same')

First, you can normalize the scores into a similar data structure as my_stats .首先,您可以将scores标准化为与my_stats类似的数据结构。 This can be done like:这可以像这样完成:


>> _scores = dict(zip([item["names"] for item in a], [int(item["power"]) for item in a])
>> _scores
{'football': 6, 'soccer': 5}

Then get from the dictionary as usual:然后像往常一样从字典中获取:

>> _scores.get("soccer")
5

暂无
暂无

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

相关问题 如何获取包含字典的值和键的列表? - How to get a list containing the values and the keys of a dictionary? 如何从另一个python字典中的一个字典中获得相应的列表值,在列表中它们被列为键,比较并打印出csv? - How can I get corresponding list values in one dictionary from another python dictionary where they are listed as keys, compare and print out a csv? 整rick:如何将字典值(包含列表)与列表进行比较? - Tricky: How to compare Dictionary values (containing lists) with a list? 如何比较字典中的值? - How do I compare values in a dictionary? 将列表与字典中的值进行比较 - compare a list with values in dictionary Python 3-如何比较一个字典中的键与另一个字典中的键,添加它们的值,将结果存储在第一个的值中? - Python 3- How do I compare keys in one dictionary to another, add their values, storing the result in the value of the first? 如何从包含字典对象的列表中删除文本? - How do I remove text from a list containing dictionary objects? 如何制作包含字典的列表的浅表副本? - How do i make a shallow copy of a list containing a dictionary? 在 Python 中,我将如何比较一个字典中的两个值在另一个字典中具有相同的值? - In Python how would I compare 2 values in one dictionary that have the same values in another dictionary? 如何比较映射到字典中键的值和列表中的元素以避免重复? - How do I compare the values mapped to keys in a dictionary with elements in list to avoid redundancy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM