[英]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.