Manipulating a list of dictionaries - python

I successfully imported from the web this json file, which looks like:
[{"h_mag":"19.7","i_deg":"9.65","moid_au":"0.035"},{"h_mag":"20.5","i_deg":"14.52","moid_au":"0.028"},
etc ...
I want to extract the values of the key moid_au, later compare moid_au with the key values of h_mag.
This works: print(data[1]['moid_au']), but if I try to ask all the elements of the list it won't, I tried: print(data[:]['moid_au']).
I tried iterators and a lambda function but still has not work yet, mostly because I'm new in data manipulation. It works when I have one dictionary, not with a list of dictionaries.
Thanks in advance for other tips. Some links were confusing.

Sounds like you are using lambda wrong because you need map as well:
c = [{"h_mag":"19.7","i_deg":"9.65","moid_au":"0.035"},{"h_mag":"20.5","i_deg":"14.52","moid_au":"0.028"}]
list(map(lambda rec: rec.get('moid_au'), c))
['0.035', '0.028']
Each lambda grabs a record from your list and you map your function to that.

Using print(data[:]['moid_au']) equals to print(data['moid_au']), and you can see that it won't work, as data has no key named 'moid_au'.
Try working with a loop:
for item in data:
print(item['moid_au'])

using your approach to iterate over the whole array to get all the instances of a key,this method might work for you
a = [data[i]['moid_au']for i in range(len(data))]
print(a)

In which exact way do you want to compare them?
Would it be useful getting the values in a way like this?
list_of_dicts = [{"h_mag":"19.7","i_deg":"9.65","moid_au":"0.035"}, {"h_mag":"20.5","i_deg":"14.52","moid_au":"0.028"}]
mod_au_values = [d["moid_au"] for d in list_of_dicts]
h_mag_values = [d["h_mag"] for d in list_of_dicts]

for key, value in my_list.items ():
print key
print value
for value in my_list.values ():
print value
for key in my_list.keys():
print key

Related

Convert a dictionary to accessible array in Python

How can I convert the following dictionary which contains an array within an array: to an array easily so I can access for example array[0]
{'New Beton': [`'C:\\User\\New map\\Test1.jpg',`'C:\\User\\New map\\Test2.jpg', 'C:\\User\\New map\\Test3.jpg']}
Which I need to convert to
New Beton = ["C:\\User\\New map\\Test1.jpg", "C:\\User\\New map\\Test2.jpg", "C:\\User\\New map\\Test3.jpg"]
Just access it directly.
you_dict['New Beton'][0]
And make sure your variable names don't have whitespace. I think except 'Rockstar' no language allows that.
Do you want to convert dictionary into a nested list? Then, something like this will work.
def convert(d):
l = []
for k, v in d.items():
l.append(v)
return l
d = {'foo':['bar', 'baz']}
l = convert(d)
print(l[0])
but there are better ways to get that value without creating a list. it'd ve great if you could share more details about what you want to do so that i can give you specific examples.

How can I get the key value pair to create a dictionary from a nested dictionary?

I have a dictionary which looks as shown below. Now I need to get the key its corresponding path together so as to use it further to identify its slot number based on the key. How can I achieve that?
I tried an approach but it is giving me key error.
What you need can easily be implemented as:
>>> {key: value["mpath"] for key, value in multipath.items()}
{'/dev/sdh': '/dev/mapper/mpathk', '/dev/sdi': '/dev/mapper/mpathk',
'/dev/sdg': '/dev/mapper/mpathj', '/dev/sdf': '/dev/mapper/mpathj',
'/dev/sdd': '/dev/mapper/mpathi', '/dev/sde': '/dev/mapper/mpathi',
'/dev/sdb': '/dev/mapper/mpathh', '/dev/sdc': '/dev/mapper/mpathh',
'/dev/sdj': '/dev/mapper/mpathg', '/dev/sdk': '/dev/mapper/mpathg'}
Great one line answer by #Selcuk using dictionary comprehension.
An elaborated one along the same line would be:
mpath_dict = {}
for sd, mpath in multipath.items():
mpath_dict[sd] = mpath['mpath']
print(mpath_dict)
Since every value item of "mpath" dictionary is a dictionary itself, you can retrieve values from it as you would do it in a dictionary.

How to make a dictionary value the key of another value?

I have an ajax POST that sends a dictionary from javascript to my Flask back-end like this:
{'output[0][description]': ['Source File'],
'output[0][input]': ['Some_document.pdf'],
'output[1][description]': ['Name'],
'output[1][input]': ['Ari'],
'output[2][description]': ['Address'],
'output[2][input]': ['12 fake st']}
So I am trying to reorganize it on the back-end to look like this:
['Source File']:['Some_document.pdf'],
['Name']:['Ari],
['Address']:['12 fake st'],
Any ideas?
One problem : You can't use a list as the key of the dict because it's not hashable.
You could use the regular expression module (re) to examine each key to determine if it conforms to the expression
output\[(\d+)\]\[description\]
for each one that does, find the corresponding key
output[$1][input]
put them together in the final dict.
The following is a sketch:
import re
P=re.compile('output\[(\d+)\]\[description\]')
inp = {'output[0][description]': ['Source File'], 'output[0][input]': ['Some_document.pdf'],
'output[1][description]': ['Name'], 'output[1][input]': ['Ari'],
'output[2][description]': ['Address'], 'output[2][input]': ['12 fake st']}
out = {}
for key in inp :
m = P.fullmatch(key)
if m :
out[inp[key][0]] = inp['output['+str(m.group(1))+'][input]'][0]
print(out)
I agree with #Klaus D.'s comment, you need to reorganize your API to use JSONs, that would simplify things but until then the following solution would be a lot faster than using regex and deliver the expected output
i=0
for key,val in inp.items():
if i<3:
print(f"{inp['output['+str(i)+'][description]']}:{inp['output['+str(i)+'][input]']}")
i+=1

Passing dictionary value into pandas.dataframe.query reutrns UndefinedVariableError

I have a dictionary like such:
dict = {'x':[2,6,4],'y':[56,5,1]}
I would like to pass one of these lists into the query method:
new_df = df.query('col3 == #dict["x"]')
But I get a UndefinedVariableError. Is there any way to do what I want without the roundabout step of setting a new variable and then using "#" with that one
new_v = dict['x']
new_df = df.query('col3 == #new_v')
Do you definetly need the query function?
Otherwise following the example in the query docs you could try something like:
df.loc[df['col3'].isin(dict['x'])]
Note that the isin, might be required since your dictionary returns a list
So you want something like ?
df.query('col3=={}'.format(dict['x']))

Python: How to traverse a List[Dict{List[Dict{}]}]

I was just wondering if there is a simple way to do this. I have a particular structure that is parsed from a file and the output is a list of a dict of a list of a dict. Currently, I just have a bit of code that looks something like this:
for i in xrange(len(data)):
for j, k in data[i].iteritems():
for l in xrange(len(data[i]['data'])):
for m, n in data[i]['data'][l].iteritems():
dostuff()
I just wanted to know if there was a function that would traverse a structure and internally figure out whether each entry was a list or a dict and if it is a dict, traverse into that dict and so on. I've only been using Python for about a month or so, so I am by no means an expert or even an intermediate user of the language. Thanks in advance for the answers.
EDIT: Even if it's possible to simplify my code at all, it would help.
You never need to iterate through xrange(len(data)). You iterate either through data (for a list) or data.items() (or values()) (for a dict).
Your code should look like this:
for elem in data:
for val in elem.itervalues():
for item in val['data']:
which is quite a bit shorter.
Will, if you're looking to decend an arbitrary structure of array/hash thingies then you can create a function to do that based on the type() function.
def traverse_it(it):
if (isinstance(it, list)):
for item in it:
traverse_it(item)
elif (isinstance(it, dict)):
for key in it.keys():
traverse_it(it[key])
else:
do_something_with_real_value(it)
Note that the average object oriented guru will tell you not to do this, and instead create a class tree where one is based on an array, another on a dict and then have a single function to process each with the same function name (ie, a virtual function) and to call that within each class function. IE, if/else trees based on types are "bad". Functions that can be called on an object to deal with its contents in its own way "good".
I think this is what you're trying to do. There is no need to use xrange() to pull out the index from the list since for iterates over each value of the list. In my example below d1 is therefore a reference to the current data[i].
for d1 in data: # iterate over outer list, d1 is a dictionary
for x in d1: # iterate over keys in d1 (the x var is unused)
for d2 in d1['data']: # iterate over the list
# iterate over (key,value) pairs in inner most dict
for k,v in d2.iteritems():
dostuff()
You're also using the name l twice (intentionally or not), but beware of how the scoping works.
well, question is quite old. however, out of my curiosity, I would like to respond to your question for much better answer which I just tried.
Suppose, dictionary looks like: dict1 = { 'a':5,'b': [1,2,{'a':100,'b':100}], 'dict 2' : {'a':3,'b':5}}
Solution:
dict1 = { 'a':5,'b': [1,2,{'a':100,'b':100}], 'dict 2' : {'a':3,'b':5}}
def recurse(dict):
if type(dict) == type({}):
for key in dict:
recurse(dict[key])
elif type(dict) == type([]):
for element in dict:
if type(element) == type({}):
recurse(element)
else:
print element
else:
print dict
recurse(dict1)

Categories