Home > Mobile >  Get dicts from array of dictionaries with the same value
Get dicts from array of dictionaries with the same value

Time:11-18

I have an array of dictionaries (example here):

data = [
  {
    "fname" : "John",
    "lname"  : "Doe",
    "country" : [ "England" ]
  },
  {
    "fname" : "Jane",
    "lname"  : "Smith",
    "country" : [ "Germany" ]
  },
  {
    "fname" : "John",
    "lname"  : "Foo",
    "country" : [ "England", "Germany" ]
  },
  {
    "fname" : "Jane",
    "lname"  : "Bar",
    "country" : [ "Germany" ]
  }
]

I need the output to be sorted, and filtered by the country and as a string:

England: John Doe, John Foo
Germany: Jane Bar, John Foo, Jane Smith

Right now, I have only figured out to find the unique countries:

values_from_country = [a_dict['country'] for a_dict in data]
unique_country = set([x for l in values_from_aff for x in l])
>>> output {'England', 'Germany'}

Can you guys help me with the next step? :-)

Kind regards

EDIT: MY FUNCTION RIGHT NOW:

def personInCountry(data)
    newdic = {}
    for dic in data:
        for country in dic['country']:
            if not country in newdic:
               newdic[country] = []
            newdic[country].append('{} {}'.format(dic['fname'], dic['lname']))

    for k, v in newdic.items():
        print('{}: {}'.format(k, ', '.join(sorted(v, key=lambda x: x.split( [-1]))))

And when I run the function with print (the may not be in the function), the output is:

print(person_to_string(data))
['England: John Doe, John Foo', 'Germany: Jane Bar, John Foo, Jane Smith']

and I want the print of the function to be:

print(personInCountry(data))

TU Wien: John Doe, John Foo
University of Vienna: Jane Bar, John Foo, Jane Smith

What is the next step for solving this? :-)

CodePudding user response:

Just loop and collect the names

from collections import defaultdict

holder = defaultdict(list)
data = [
  {
    "fname" : "John",
    "lname"  : "Doe",
    "country" : [ "England" ]
  },
  {
    "fname" : "Jane",
    "lname"  : "Smith",
    "country" : [ "Germany" ]
  },
  {
    "fname" : "John",
    "lname"  : "Foo",
    "country" : [ "England", "Germany" ]
  },
  {
    "fname" : "Jane",
    "lname"  : "Bar",
    "country" : [ "Germany" ]
  }
]

for d in data:
  for c in d['country']:
    holder[c].append(d["fname"]   ' '    d["lname"])
for k,v in holder.items():
  print(f'{k}: {", ".join(v)}')

output

England: John Doe, John Foo
Germany: Jane Smith, John Foo, Jane Bar

CodePudding user response:

It's quite simple to just loop over the data, generating a new dictionary keyed by country with names in a list:

data = [
  {
    "fname" : "John",
    "lname"  : "Doe",
    "country" : [ "England" ]
  },
  {
    "fname" : "Jane",
    "lname"  : "Smith",
    "country" : [ "Germany" ]
  },
  {
    "fname" : "John",
    "lname"  : "Foo",
    "country" : [ "England", "Germany" ]
  },
  {
    "fname" : "Jane",
    "lname"  : "Bar",
    "country" : [ "Germany" ]
  }
]

newdic = {}
for dic in data:
    for country in dic['country']:
        if not country in newdic:
            newdic[country] = []
        newdic[country].append('{} {}'.format(dic['fname'], dic['lname']))

print(newdic)
# {'England': ['John Doe', 'John Foo'], 'Germany': ['Jane Smith', 'John Foo', 'Jane Bar']}

You can then choose how to sort/output this data - e.g. to sort the names by surname:

for k, v in newdic.items():
    print('{}: {}'.format(k, ', '.join(sorted(v, key=lambda x: x.split()[-1]))))

# England: John Doe, John Foo
# Germany: Jane Bar, John Foo, Jane Smith
  • Related