How to get values from dictionary in python? - python

I want to add value of entity_id with attribute for type which is String.
Below are the details of code:
entities = [{'id': 'Room1',
'temperature': {'value': '10', 'type': 'String'},
'pressure': {'value': '12', 'type': 'Number'},
'type': 'Room',
'time_index': '2020-08-24T06:23:37.346348'},
{'id': 'Room2',
'temperature': {'value': '10', 'type': 'Number'},
'pressure': {'value': '12', 'type': 'Number'},
'type': 'Room',
'time_index': '2020-08-24T06:23:37.346664'},
{'id': 'Room3',
'temperature': {'value': '10', 'type': 'Number'},
'pressure': {'value': '12', 'type': 'Number'},
'type': 'Array',
'time_index': '2020-08-24T06:23:37.346664'}]
attr = ['temperature','pressure']
self.logger.warning(msg.format( attr, entity_id)
How can i add value of id where type is String with warning msg.format in above code..
New to python so any help on it will be great.Thanks

To get the value of a dictionary key you do the following.
dict = {'a':1, 'b':2}
print(dict['a'])
This will print the result 1
If you have a dictionary inside a dictionary then do the following
dict = {1: {'a': 'abc', 'b': 'xyz'}}
print(dict[1][a])
This will print abc
Another alternative to putting dictionaries inside dictionaries would be to make a particular key hold a tuple, look at the following example
dict = {'a': ('abc', 'xyz'), 'b':('qwe', 12)}
for i in dict:
print(dict[i][1])
This will iterate through your dictionary and print xyz and 12. You could also give a condition to print it only if it meets a particular age by following this.
You can also iterate through your nested dictionaries if you desire that.
Depending on your need you can make it hold a dictionary or a tuple however, I think a tuple makes it easier to read and understand code (this is my preference).

Related

Make a list consistent

I have 2 list ..
a = [{'Work': 'R'}, {'Area': 'S0'}, {'Type': 'PIV'}, {'Disc': 'LA'}, {'L': 'A'}]
b = [{'Key': '9', 'FileName': '123A.pdf', 'Code': '1', 'Type': 'PNHRG'}]
output -- [{'Key': '9', 'FileName': '123A.pdf', 'Code': '1', 'Type': 'PNHRG','Work': 'R','Area': 'S0','Type': 'PIV','Disc': 'LA','L': 'A'}]
I tried 'extend','append','insert' but did not get desired
output. tried all most all list operations with loops too. I am
not in position to change any kind of types for this.
Tried this solution too without luck. not sure where I am missing.
How do I make a flat list out of a list of lists?
You can use a doubly-nested dictionary comprehension, iterating over all the items in all the dicts in all the lists. Assuming multiple dicts in b might not be necessary, but makes it easier.
>>> a = [{'Work': 'R'}, {'Area': 'S0'}, {'Type': 'PIV'}, {'Disc': 'LA'}, {'L': 'A'}]
>>> b = [{'Key': '9', 'FileName': '123A.pdf', 'Code': '1', 'Type': 'PNHRG'}]
>>> [{k: v for lst in (a, b) for d in lst for k, v in d.items()}]
[{'Work': 'R', 'Area': 'S0', 'Type': 'PNHRG', 'Disc': 'LA', 'L': 'A', 'Key': '9', 'FileName': '123A.pdf', 'Code': '1'}]
Addendum: If there are any keys that appear in more than one dictionary, as is the case with "type" here, the value from the last dictionary that was iterated in the comprehension will be used, i.e. b here. If you need the value from a, do ... for lst in (b, a).... In your expected output, that key appears twice, but that is not possible unless you change the format, as in the other answer.
Extra from the answer provided by tobias_k:
If you want to combine both lists of dictionaries (and more when you have it) as a dictionary with a list of values in the same key. You can do this.
from collections import defaultdict
mydict = defaultdict(list)
for i in (a+b):
for key, value in i.items():
mydict[key].append(value)
mydict
Out[32]:
defaultdict(list,
{'Work': ['R'],
'Area': ['S0'],
'Type': ['PIV', 'PNHRG'],
'Disc': ['LA'],
'L': ['A'],
'Key': ['9'],
'FileName': ['123A.pdf'],
'Code': ['1']})

Adding key and value to dictionary in python based on other dictionaries

I am using for loop in python and every loop creates a dictionary. I have the below set of dictionaries created.
{'name': 'xxxx'}
{'name': 'yyyy','age':'28'}
{'name': 'zzzz','age':'27','sex':'F'}
My requirement is to compare all the dictionaries created and find out the missing key values and add the key to missing dictionaries and order every dictionary based on key. Below is the expected output
Expected output:
{'age':'','name': 'xxxx','sex':''}
{'age':'28','name': 'yyyy','sex':''}
{'age':'27','name': 'zzzz','sex':'F'}
How to achieve this in python.
If you want to modify the dicts in-place, dict.setdefault would be easy enough.
my_dicts = [
{'name': 'xxxx'},
{'name': 'yyyy','age':'28'},
{'name': 'zzzz','age':'27','sex':'F'},
]
desired_keys = ['name', 'age', 'sex']
for d in my_dicts:
for key in desired_keys:
d.setdefault(key, "")
print(my_dicts)
prints out
[
{'name': 'xxxx', 'age': '', 'sex': ''},
{'name': 'yyyy', 'age': '28', 'sex': ''},
{'name': 'zzzz', 'age': '27', 'sex': 'F'},
]
If you don't want to hard-code the desired_keys list, you can make it a set and gather it from the dicts before the loop above.
desired_keys = set()
for d in my_dicts:
desired_keys.update(set(d)) # update with keys from `d`
Another option, if you want new dicts instead of modifying them in place, is
desired_keys = ... # whichever method you like
empty_dict = dict.fromkeys(desired_keys, "")
new_dicts = [{**empty_dict, **d} for d in my_dicts]
EDIT based on comments:
This doesn't remove keys that are not there in desired keys.
This will leave only the desired keys:
desired_keys = ... # Must be a set
for d in my_dicts:
for key in desired_keys:
d.setdefault(key, "")
for key in set(d) - desired_keys:
d.pop(key)
However, at that point it might be easier to just create new dicts:
new_dicts = [
{key: d.get(value, "") for key in desired_keys}
for d in my_dicts
]
data = [{'name': 'xxxx'},
{'name': 'yyyy','age':'28'},
{'name': 'zzzz','age':'27','sex':'F'}]
First get the maximum, to get all the keys.
Then use dict.get to get default value as empty string for each of the keys, and sort the dictionary on key, you can combine List-comprehension and dict-comprehension:
allKD = max(data, key=len)
[dict(sorted({k:d.get(k, '') for k in allKD}.items(), key=lambda x:x[0])) for d in data]
OUTPUT:
[{'age': '', 'name': 'xxxx', 'sex': ''},
{'age': '28', 'name': 'yyyy', 'sex': ''},
{'age': '27', 'name': 'zzzz', 'sex': 'F'}]
One approach:
from operator import or_
from functools import reduce
lst = [{'name': 'xxxx'},
{'name': 'yyyy', 'age': '28'},
{'name': 'zzzz', 'age': '27', 'sex': 'F'}]
# find all the keys
keys = reduce(or_, map(dict.keys, lst))
# update each dictionary with the complement of the keys
for d in lst:
d.update(dict.fromkeys(keys - d.keys(), ""))
print(lst)
Output
[{'name': 'xxxx', 'age': '', 'sex': ''}, {'name': 'yyyy', 'age': '28', 'sex': ''}, {'name': 'zzzz', 'age': '27', 'sex': 'F'}]

Python Re-Arranging Dictionary for Repetitive Category Names from SQL Query to a Single Category Name as Key

So I am trying to achieve a dictionary result like:
{'foo': ({'key': 'value'}, {'key': 'value1'})}
So far I am only able achieve this result with this code:
maindict = {}
dict1 = {'key': 'value'}
dict2 = {'key': 'value1'}
maindict['foo'] = dict1, dict2
print(maindict)
But I can't use it in a for loop. Tried update() dictionary function, it just overwrites the dictionary.
Is there a way around?
EDITED
Alright folks so here is the original query.
Query returns this:
Now in python this is how it looks like from sql query:
[{'id': 1, 'url': '/static/images/dresses/td1.jpg', 'price': 3000,
'name': 'product1', 'catname': 'Linen', 'catid': 1}, {'id': 4, 'url':
'/static/images/dresses/td4.jpg', 'price': 5000, 'name': 'product4',
'catname': 'Linen', 'catid': 1}, {'id': 2, 'url':
'/static/images/dresses/td2.jpg', 'price': 2500, 'name': 'product2',
'catname': 'Chiffron', 'catid': 2}, {'id': 3, 'url':
'/static/images/dresses/td3.jpg', 'price': 4000, 'name': 'product3',
'catname': 'Chiffron', 'catid': 2}, {'id': 5, 'url':
'/static/images/dresses/td5.jpg', 'price': 6000, 'name': 'product6',
'catname': 'Chiffron', 'catid': 2}]
I am trying to re-arrange this list of dictionaries in a for loop in a way that every product and its info as value gets nested inside a single key where key is catname(product's category) for instance for Linen category I want it rearranged like:
{'Linen': ({'name': 'product1', 'Price':'xxx'....}, {'name': 'product4', 'Price': 'xxx'}...)}
same goes for Chiffron or any category from the query.
This is how I want to sort query rows in python dictionary. As you can see catname(category of products) has been repeated many times in query. I want to cut that repetition. I want one dictionary for each distinct category and sort the products ands its info under their category by having nested dictionary.
You can use a collections.defaultdict to collect the rows in lists, grouped by the category name.
import collections
maindict = collections.defaultdict(list)
for row in rows:
maindict[row['catname']].append(row)
for value in maindict['Linen']:
print(value)
{'id': 1, 'url': '/static/images/dresses/td1.jpg', 'price': 3000, 'name': 'product1', 'catname': 'Linen', 'catid': 1}
{'id': 4, 'url': '/static/images/dresses/td4.jpg', 'price': 5000, 'name': 'product4', 'catname': 'Linen', 'catid': 1}
You could do the same with a normal dict, but you would need to handle creating the initial list each time you encounter a new key; defaultdict takes care of this for you.

How can i insert a list of dictionaries into my existing item

I want to insert something in Python 3.8 with Pymongo
I got a short question. I have a list of dictionaries called items:
item_list = [{'name': 'abc', 'category': 'cde', 'assurance': 0.9844486508518457, 'count': 1},
{'name': 'abc', 'category': 'cde', 'assurance': 0.7428154349327087, 'count': 1},
{'name': 'abc', 'category': 'cde', 'assurance': 0.9954652488231659, 'count': 1}]
I also have my database item:
item = collection.find_one(<placeholder>)
Now I tried the following:
collection.update_one(item, {"$set": {"Entity": item_list} }, True)
But this does not work... How can I insert a list of dictionaries?
Thank you.

How to convert nested dict to nested list to find value

I have a nested dict my_dict in python:
{'version': '1.1',
'time': '1520541234',
'car': [{'id': '123', 'locationX': '-0.673606', 'locationY': '23.5310399', '#id': 'http://URL', 'standardname': 'value1', 'name': 'value_1'},
{'id': '124', 'locationX': '0.O683', 'locationY': '48.42', '#id': 'htt://URL2', 'standardname': 'value2', 'name': 'value_2'},
{'id': '125', 'locationX': '42.532', 'locationY': '12.424', '#id': 'http://URL3', 'standardname': 'value3', 'name': 'value_3'},
....
I want to check if one specific value is in the dic. I saw it seems easier in a list so I try to convert my my_dict to a list:
my_list=(list(my_dict.items()))
Now I'm searching for my value
any('value' in sl for sl in my_list)
But this does not work. What am I missing here?

Categories

Resources