How to get maximum value of one entity in nested dictionary? - python

people = {1: {'Name': 'John', 'Age': '22', 'Sex': 'Male'}, 2: {'Name': 'Marie', 'Age': '26', 'Sex': 'Female'}, 3: {'Name': 'Marie', 'Age': '25', 'Sex': 'Female'}, 4: {'Name': 'Marie', 'Age': '21', 'Sex': 'Female'}}
I want to get the maximum value of 'Age'. Kindly help me how to do this.

You can use max with a defined key and lambda.
people = {1: {'Name': 'John', 'Age': '22', 'Sex': 'Male'}, 2: {'Name': 'Marie', 'Age': '26', 'Sex': 'Female'}, 3: {'Name': 'Marie', 'Age': '25', 'Sex': 'Female'}, 4: {'Name': 'Marie', 'Age': '21', 'Sex': 'Female'}}
max(people.values(), key=lambda x: int(x['Age']))
# {'Name': 'Marie', 'Age': '26', 'Sex': 'Female'}
max(people.values(), key=lambda x: int(x['Age']))['Age']
# '26'

If you wanted just the max value then max with a generator would suffice
people = {1: {'Name': 'John', 'Age': '22', 'Sex': 'Male'}, 2: {'Name': 'Marie', 'Age': '26', 'Sex': 'Female'}, 3: {'Name': 'Marie', 'Age': '25', 'Sex': 'Female'}, 4: {'Name': 'Marie', 'Age': '21', 'Sex': 'Female'}}
result = max(int(p['Age']) for p in people.values())
Which I prefer over the lambda, however if you want the dictionary that has the max Age then #I'mahdi answer is what you want.

Related

How to alphabetically sort a nested dictionary by key without using OrderedDict

Say I have this nested dictionary:
people = {
Kamara: {'sex': 'Male', 'alias': 'John', 'age': '27'},
Alison: {'age': '21', 'alias': 'Marie', 'sex': 'Female'},
Oliver: {'alias': 'Marie', 'age': '25', 'sex': 'Female'}
}
I want to alphabetically sort everything. I mean not only the main keys, but also the sub-keys, without using the OrderedDict library. The final result should be this:
people = {
Alison: {'age': '21', 'alias': 'Marie', 'sex': 'Female'},
Kamara: {'age': '27', 'alias': 'John', 'sex': 'Male'},
Oliver: {'age': '25', 'alias': 'Marie', 'sex': 'Female'}
}
This is what I have:
people = {key: dict(sorted(val.items(), key=lambda ele: ele[1])) for key, val in people.items()}
but I get this error:
'str' object has no attribute 'items'
Dict comprehension can do this.
people = {
'Kamara': {'sex': 'Male', 'alias': 'John', 'age': '27'},
'Alison': {'age': '21', 'alias': 'Marie', 'sex': 'Female'},
'Oliver': {'alias': 'Marie', 'age': '25', 'sex': 'Female'}
}
new_dict = {k: {x:y for x,y in sorted(v.items())} for k,v in sorted(people.items())}
print(new_dict)
{'Alison': {'age': '21', 'alias': 'Marie', 'sex': 'Female'},
'Kamara': {'age': '27', 'alias': 'John', 'sex': 'Male'},
'Oliver': {'age': '25', 'alias': 'Marie', 'sex': 'Female'}}
f = = {k: dict(sorted(v.items(), key=lambda item: item[0])) for k, v in sorted(people.items(), key=lambda item: item[0]) }
Output:
{'Alison': {'age': '21', 'alias': 'Marie', 'sex': 'Female'},
'Kamara': {'age': '27', 'alias': 'John', 'sex': 'Male'},
'Oliver': {'age': '25', 'alias': 'Marie', 'sex': 'Female'}}

Filter nested dictionary in Python

I'm trying to remove key: value pairs from a nested dictionary within a nested dictionary, based on the value of a value within the double-nested dict.
The dictionary looks something like this, and I want to filter out entire entries of people with an age under 25 years old (while I do not want to filter out the outermost dictionary, so the "people group" one).
# Make a nested dictionary for test
people = {0:{1:{'name': 'John', 'age': '27', 'gender': 'Male'},
2: {'name': 'Marie', 'age': '22', 'gender': 'Female'},
3: {'name': 'Nicola', 'age': '19', 'gender': 'Non-binary'},
4: {'name': 'Garfield', 'age': '32', 'gender': 'Male'}},
1:{1:{'name': 'Katie', 'age': '24', 'gender': 'Male'},
2: {'name': 'Marigold', 'age': '42', 'gender': 'Female'},
3: {'name': 'James', 'age': '10', 'gender': 'Non-binary'},
4: {'name': 'Precious', 'age': '35', 'gender': 'Male'}}}
I have found my way to this thread, which is somewhat similar, although there's only one layer of "nestedness" there.
From it, I learnt that I could do something like this to filter keys with too low values tied to them, if my dictionary had only been nested one round:
{i:j for i,j in people.items() if j.get('age',0) >='25'}
How can I reach the element within a double-nested dictionary like this, and then remove the whole "single-nested dictionary", but keep the outermost one?
You can use nested dict comprehension:
>>> {gid: {uid: user for uid, user in pg.items() if int(user.get('age', 0)) >= 25} for gid, pg in people.items()}
{0: {1: {'name': 'John', 'age': '27', 'gender': 'Male'},
4: {'name': 'Garfield', 'age': '32', 'gender': 'Male'}},
1: {2: {'name': 'Marigold', 'age': '42', 'gender': 'Female'},
4: {'name': 'Precious', 'age': '35', 'gender': 'Male'}}}

Get specific the nested key/values based on a condition from python nested dictionary

I'm stuck parsing the below python nested dictionary based on the nested key. I want to filter a key's value and return all the nested key/values related to that.
{ 'US': { 'Washington': {'Seattle': {1: {'name': 'John', 'age': '27', 'gender': 'Male'}}},
{ 'Florida': {'some city': {2: {'name': 'Marie', 'age': '22', 'gender': 'Female'}}},
{ 'Ohio': {'some city': {3: {'name': 'Luna', 'age': '24', 'gender': 'Female', 'married': 'No'}}},
{ 'Nevada': {'some city': {4: {'name': 'Peter', 'age': '29', 'gender': 'Male', 'married': 'Yes'}}}}}
For instance, filtering on gender "Male" should return the below:
US
Washington
Seattle
1
name:John
age: 27
US
Nevada
somecity
4
name:Peter
age: 29
married: Yes
Can you please suggest the best way to parse it. I tried to use contains within a loop that doesn't seem to work.
We can recursively explore the dict structure, keeping track of the path of keys at each point. When we reach a dict containing the target value, we yield the path and the content of the dict.
We can use this generator:
def recursive_search(dct, target, path=None):
if path is None:
path = []
if target in dct.values():
out = ' '.join(path) + ' ' + ' '.join(f'{key}:{value}' for key, value in dct.items())
yield out
else:
for key, value in dct.items():
if isinstance(value, dict):
yield from recursive_search(value, target, path+[str(key)])
this way:
data = { 'US': { 'Washington': {'Seattle': {1: {'name': 'John', 'age': '27', 'gender': 'Male'}}},
'Florida': {'some city': {2: {'name': 'Marie', 'age': '22', 'gender': 'Female'}}},
'Ohio': {'some city': {3: {'name': 'Luna', 'age': '24', 'gender': 'Female', 'married': 'No'}}},
'Nevada': {'some city': {4: {'name': 'Peter', 'age': '29', 'gender': 'Male', 'married': 'Yes'}}}}}
for match in recursive_search(data, 'Male'):
print(match)
# US Washington Seattle 1 name:John age:27 gender:Male
# US Nevada some city 4 name:Peter age:29 gender:Male married:Yes
This Code Will work...
a_dict={ 'US': { 'Washington': {'Seattle': {1: {'name': 'John', 'age': '27', 'gender': 'Male'}}}, 'Florida': {'some city': {2: {'name': 'Marie', 'age': '22', 'gender': 'Female'}}}, 'Ohio': {'some city': {3: {'name': 'Luna', 'age': '24', 'gender': 'Female', 'married': 'No'}}}, 'Nevada': {'some city': {4: {'name': 'Peter', 'age': '29', 'gender': 'Male', 'married': 'Yes'}}}}}
for k,v in a_dict.items():
for k1,v1 in v.items():
for k2,v2 in v1.items():
for k3,v3 in v2.items():
if v3["gender"]=="Male":
string=""
for k4,v4 in v3.items():
string=string+ k4+":"+v4+" "
print(k,k1,k2,k3, string.strip())

how do i remove a duplicate lines in a list using a function?

def remove_repeated_lines(data):
lines_seen = set() # holds lines already seen
d=[]
for t in data:
if t not in lines_seen: # check if line is not duplicate
d.append(t)
lines_seen.add(t)
return d
a=[{'name': 'paul', 'age': '26.', 'hometown': 'AU', 'gender': 'male'},
{'name': 'mei', 'age': '26.', 'hometown': 'NY', 'gender': 'female'},
{'name': 'smith', 'age': '16.', 'hometown': 'NY', 'gender': 'male'},
{'name': 'raj', 'age': '13.', 'hometown': 'IND', 'gender': 'male'}]
age=[]
for line in a:
for key,value in line.items():
if key == 'age':
age.append(remove_repeated_lines(value.replace('.','___')))
print(age)
the output is
[['2', '6', '___'], ['2', '6', '___'], ['1', '6', '___'], ['1', '3', '___']]
my desired output is ['26___','16___','13___']
Here is my code to remove repeated lines from the value of a dictionary. After I run the code, the repeated lines are not remove.
In [37]: a=[{'name': 'paul', 'age': '26.', 'hometown': 'AU', 'gender': 'male'},
...: {'name': 'mei', 'age': '26.', 'hometown': 'NY', 'gender': 'female'},
...: {'name': 'smith', 'age': '16.', 'hometown': 'NY', 'gender': 'male'},
...: {'name': 'raj', 'age': '13.', 'hometown': 'IND', 'gender': 'male'}]
In [40]: set(i["age"].replace(".","")+"_" for i in a)
Out[40]: {'13_', '16_', '26_'}
You can use set comprehension to do it with ease, in a more readable fashion:
age = list({
line['age'].replace('.', '___')
for line in a
if 'age' in line
})
Output:
['26___', '16___', '13___']

Python - Zip 2 lists into 1 dictionary

I'm making some scrip with Python and having one small question.
I have 2 lists:
['name', 'age', 'sex', 'addr', 'city']
['Jack 24 male no23 NY', 'Jane 25 female no24 NY', 'Dane 14 male no14 NY']
So I want to have:
dictofJack = {'name': 'Jack', 'age': '24', 'sex': 'male', 'addr': 'no23', 'city':'NY'}
dictofJane = {'name': 'Jane', 'age': '25', 'sex': 'female', 'addr': 'no24', 'city':'NY'}
dictofDane = {'name': 'Dane', 'age': '14', 'sex': 'male', 'addr': 'no14', 'city':'NY'}
In this case, how can I use zip to make it get the dictionaries automatically in a for loop?
Using list comprehension or generator expression:
>>> header = ['name', 'age', 'sex', 'addr', 'city']
>>> values = ['Jack 24 male no23 NY',
'Jane 25 female no24 NY',
'Dane 14 male no14 NY']
>>> dictofJack, dictofJane, dictofDane = (
dict(zip(header, value.split())) for value in values
)
>>> dictofJack
{'addr': 'no23', 'age': '24', 'city': 'NY', 'name': 'Jack', 'sex': 'male'}
>>> dictofJane
{'addr': 'no24', 'age': '25', 'city':'NY', 'name': 'Jane', 'sex': 'female'}
>>> dictofDane
{'addr': 'no14', 'age': '14', 'city': 'NY', 'name': 'Dane', 'sex': 'male'}
BTW, instead of making multiple variables of dictionaries, I recommend to use dictionary of dictionaries (think of case where 100 of dictionaries required), using dictionary comprehension:
>>> {value.split()[0]: dict(zip(header, value.split())) for value in values}
{'Jane': {'addr': 'no24', 'age': '25', 'city': 'NY', 'name': 'Jane', 'sex': 'female'},
'Dane': {'addr': 'no14', 'age': '14', 'city': 'NY', 'name': 'Dane', 'sex': 'male'},
'Jack': {'addr': 'no23', 'age': '24', 'city': 'NY', 'name': 'Jack', 'sex': 'male'}}

Categories