-5

Suppose if I have the following list of dictionaries:

heroes = [
            {'id':'001', 'actor_name':'Chris Evans', 'hero_name':'Captain America'},
            {'id':'002', 'actor_name':'Chris Hemsworth', 'hero_name':'Thor'},
            {'id':'003', 'actor_name':'Robert Downey', 'hero_name':'Iron Man'},
            {'id':'004', 'actor_name':'Don Cheadle', 'hero_name':'War Machine'},
            {'id':'005', 'actor_name':'Jeremy Renner', 'hero_name':'Hawkeye'},
        ]

I need to get values of the form;

"Get hero id where actor name is 'Don Cheadle' and hero name is 'War Machine'

or

"Get hero name where id is 1"

How can I achieve this in python ?

Since there are multiple dictionaries in a list, there can be some dictionaries which have the same id as well. So for that case I need all such values.

This question has got me extremely confused. How can I achieve this?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Vai
  • 343
  • 3
  • 17

3 Answers3

2

You can do this with a simple for loop in python

heroes = [
    {'id': '001', 'actor_name': 'Chris Evans', 'hero_name': 'Captain America'},
    {'id': '002', 'actor_name': 'Chris Hemsworth', 'hero_name': 'Thor'},
    {'id': '003', 'actor_name': 'Robert Downey', 'hero_name': 'Iron Man'},
    {'id': '004', 'actor_name': 'Don Cheadle', 'hero_name': 'War Machine'},
    {'id': '005', 'actor_name': 'Jeremy Renner', 'hero_name': 'Hawkeye'},
]

for hero in heroes:
    if int(hero['id']) == 1:
        print(hero['hero_name'])
    elif hero['hero_name'] == 'War Machine' and hero['actor_name'] == 'Don Cheadle':
        print(hero['id'])


OUTPUT

Captain America
004
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42
  • I thought of this approach but, the select clause can have any column name and not only the ones mentioned above. But thanks for the help and your prompt response :) – Vai Dec 30 '19 at 14:11
1

You can create a pandas.DataFrame with your data

import pandas as pd
heroes = [
            {'id':'001', 'actor_name':'Chris Evans', 'hero_name':'Captain America'},
            {'id':'002', 'actor_name':'Chris Hemsworth', 'hero_name':'Thor'},
            {'id':'003', 'actor_name':'Robert Downey', 'hero_name':'Iron Man'},
            {'id':'004', 'actor_name':'Don Cheadle', 'hero_name':'War Machine'},
            {'id':'005', 'actor_name':'Jeremy Renner', 'hero_name':'Hawkeye'},
         ]
data = pd.DataFrame(heroes)

Then for example to make a query

>>> data['actor_name'] == 'Don Cheadle'
0    False
1    False
2    False
3     True
4    False
Name: actor_name, dtype: bool

Then to use the result of a query

>>> data.loc[data['actor_name'] == 'Don Cheadle']['id']
3    004
Name: id, dtype: object

You can create boolean-type queries as well

>>> (data['actor_name'] == 'Don Cheadle') & (data['hero_name'] == 'War Machine')
0    False
1    False
2    False
3     True
4    False
dtype: bool

There is also pandas.read_sql_query that can perform SQL queries against your dataframe.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
1

I'd create a function that takes the column to return and a dictionary of conditions, and filters the records accordingly:

def find_data(returnCol, conds):
    retVal = []
    for hero in heroes:
        toAdd = True
        for k, v in conds.items():
            if not hero[k] == v:
               toAdd = False
               break
        if toAdd:
            retVal.append(hero[returnCol])

    return retVal

print(find_data('id', {'actor_name' : 'Don Cheadle', 'hero_name' : 'War Machine'}))
Mureinik
  • 297,002
  • 52
  • 306
  • 350