简体   繁体   中英

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.

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? 1 for the list and 1 for the dictionary?

I added a few more entries into the scores list to make the code more generic.

The below code will scan through the full scores list and find the max value of the unique games in 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:

{'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 = [{'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 = {'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 . 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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