Is it possible to make a function that will return a nested dict depending on the arguments?
def foo(key):
d = {'a': 1, 'b': 2, 'c': {'d': 3, 'e': 4}, }
return d[key]
foo(['c']['d'])
I waiting for:
3
I'm getting:
TypeError: list indices must be integers or slices, not str
I understanding that it possible to return a whole dict, or hard code it to return a particular part of dict, like
if 'c' and 'd' in kwargs:
return d['c']['d']
elif 'c' and 'e' in kwargs:
return d['c']['e']
but it will be very inflexible
When you give ['c']['d'], you slice the list ['c'] using the letter d, which isin't possible. So what you can do is, correct the slicing:
foo('c')['d']
Or you could alter your function to slice it:
def foo(*args):
d = {'a': 1, 'b': 2, 'c': {'d': 3, 'e': 4}, }
d_old = dict(d) # if case you have to store the dict for other operations in the fucntion
for i in args:
d = d[i]
return d
>>> foo('c','d')
3
d = {'a': 1, 'b': 2, 'c': {'d': 3, 'e': 4}, }
def funt(keys):
val = d
for key in keys:
if val:
val = val.get(key)
return val
funt(['c', 'd'])
Additionally to handle key not present state.
One possible solution would be to iterate over multiple keys -
def foo(keys, d=None):
if d is None:
d = {'a': 1, 'b': 2, 'c': {'d': 3, 'e': 4}, }
if len(keys) == 1:
return d[keys[0]]
return foo(keys[1:], d[keys[0]])
foo(['c', 'd'])
Related
I have a dictionary composed of {key: value}.
I select a set of keys from this dictionary.
I'd like to build a new dictionary with {keyA: set of all keys wich have the same value as keyA}.
I already have a solution: Is there a faster way to do it?
It seems very slow to me, and I imagine I'm not the only one in this case!
for key1 in selectedkeys:
if key1 not in seen:
seen.add(key1)
equal[key1] = set([key1])#egual to itself
for key2 in selectedkeys:
if key2 not in seen and dico[key1] == dico[key2]:
equal[key1].add(key2)
seen.update(equal[key1])
Try this
>>> a = {1:1, 2:1, 3:2, 4:2}
>>> ret_val = {}
>>> for k, v in a.iteritems():
... ret_val.setdefault(v, []).append(k)
...
>>> ret_val
{1: [1, 2], 2: [3, 4]}
def convert(d):
result = {}
for k, v in d.items(): # or d.iteritems() if using python 2
if v not in result:
result[v] = set()
result[v].add(k)
return result
or just use collections.defaultdict(set) if you are careful enough not to access any non key later :-)
So you want to create a dictionary that maps key to "the set of all keys which have the same value as key" for each selected key in a given source dictionary.
Thus, if the source dictionary is:
{'a': 1, 'b': 2, 'c': 1, 'd': 2, 'e': 3, 'f': 1, 'g': 3)
and the selected keys are a, b, and e, the result should be:
{'a': {'a', 'c', 'f'}, 'e': {'g', 'e'}, 'b': {'b', 'd'}}
One way to achieve this would be to use a defaultdict to build a value to key table, and then use that to build the required result from the specified keys:
from collections import defaultdict
def value_map(source, keys):
table = defaultdict(set)
for key, value in source.items():
table[value].add(key)
return {key: table[source[key]] for key in keys}
source = {'a': 1, 'b': 2, 'c': 1, 'd': 2, 'e': 3, 'f': 1, 'g': 3)
print(value_map(source, ['a', 'b', 'e']))
Output:
{'a': {'a', 'c', 'f'}, 'e': {'g', 'e'}, 'b': {'b', 'd'}}
Since you select a set of keys from the original dictionary. We can modify #Nilesh solution for your purpose.
a = {1:1, 2:1, 3:2, 4:2}
keys = [1, 3] # lets say this is the list of keys
ret_val = {}
for i in keys:
for k,v in a.items():
if a[i]==v:
ret_val.setdefault(i, []).append(k)
print (ret_val)
{1: [1, 2], 3: [3, 4]}
This was sort of stated in the comments by #Patrick Haugh:
d=your dictionary
s=set(d.values())
d2={i:[] for i in s}
for k in d:
d2[d[k]].append(k)
My goal is to convert a class to a dict, recursively. I understand that I need to specify the rules related to the conversion, but I feel like there is some way for built-in python to recurse the class and produce a dict. See the following code:
class Foo:
def __init__(self):
self.a = 1
self.b = 2
self.c = 3
def __iter__(self):
yield 'a', self.a
yield 'b', self.b
yield 'c', self.c
class Bar:
def __init__(self, foos):
self.foos = foos
self.d = 5
def __iter__(self):
yield 'foos', self.foos
yield 'd', self.d
foos = [Foo(), Foo(), Foo()]
bar = Bar(foos)
print(dict(bar))
The result of this is:
{'foos': [<__main__.Foo object at 0x108e6be48>, <__main__.Foo object at 0x108e6bef0>, <__main__.Foo object at 0x108e6bf28>], 'd': 5}
foos in this case is a list of Foo()s, which I would hope to see an expanded list of dicts. I know I can add the following to the Foo class to make this work:
def __repr__(self):
return repr(dict(self))
Results in the desired output:
{'foos': [{'a': 1, 'b': 2, 'c': 3}, {'a': 1, 'b': 2, 'c': 3}, {'a': 1, 'b': 2, 'c': 3}], 'd': 5}
but that approach seems hacky - as in, I'm forcing it to represent itself as a dict, rather than naturally iterable (which feels like I'm trying to put a square peg in a round hole).
My question to the python world is: what is the proper way to approach this so that I can call dict() on my class and get a full dictionary?
I've searched around and can't seem to find anything specific to this topic so I'm going to ask here. If this already exists please point me in the right direction. Thanks in advance for your help!
I've came up with two hacks that can fill your needs.
1st: Exposing dicts in foos variable by replacing this line:
foos = [Foo(), Foo(), Foo()]
by this kind of hacks using list comprehension:
foos =[dict(k) for k in [Foo(), Foo(), Foo()]]
Or, simply like this:
foos = [dict(Foo()), dict(Foo()), dict(Foo())]
then:
print(dict(bar))
>>> {'d': 5, 'foos': [{'a': 1, 'c': 3, 'b': 2}, {'a': 1, 'c': 3, 'b': 2}, {'a': 1, 'c': 3, 'b': 2}]}
2nd: Exposing dicts inside Bar.__iter__() by replacing this line:
yield 'foos', self.foos
by:
yield [dict(k) for k in self.foos]
Then:
print(dict(bar))
>>> {'d': 5, 'foos': [{'a': 1, 'c': 3, 'b': 2}, {'a': 1, 'c': 3, 'b': 2}, {'a': 1, 'c': 3, 'b': 2}]}
Bonus (not exactly what you want): Using dict comprehension by replacing this line:
print(dict(bar))
by:
print({k:v if isinstance(v, int) else [dict(j) for j in v] for k,v in dict(bar).items()})
which will print:
>>> {'d': 5, 'foos': [{'a': 1, 'c': 3, 'b': 2}, {'a': 1, 'c': 3, 'b': 2}, {'a': 1, 'c': 3, 'b': 2}]}
Is there a library that would help me achieve the task to rearrange the levels of a nested dictionary
Eg: From this:
{1:{"A":"i","B":"ii","C":"i"},2:{"B":"i","C":"ii"},3:{"A":"iii"}}
To this:
{"A":{1:"i",3:"iii"},"B":{1:"ii",2:"i"},"C":{1:"i",2:"ii"}}
ie first two levels on a 3 levelled dictionary swapped. So instead of 1 mapping to A and 3 mapping to A, we have A mapping to 1 and 3.
The solution should be practical for an arbitrary depth and move from one level to any other within.
>>> d = {1:{"A":"i","B":"ii","C":"i"},2:{"B":"i","C":"ii"},3:{"A":"iii"}}
>>> keys = ['A','B','C']
>>> e = {key:{k:d[k][key] for k in d if key in d[k]} for key in keys}
>>> e
{'C': {1: 'i', 2: 'ii'}, 'B': {1: 'ii', 2: 'i'}, 'A': {1: 'i', 3: 'iii'}}
thank god for dict comprehension
One way to think about this would be to consider your data as a (named) array and to take the transpose. An easy way to achieve this would be to use the data analysis package Pandas:
import pandas as pd
df = pd.DataFrame({1: {"A":"i","B":"ii","C":"i"},
2: {"B":"i","C":"ii"},
3: {"A":"iii"}})
df.transpose().to_dict()
{'A': {1: 'i', 2: nan, 3: 'iii'},
'B': {1: 'ii', 2: 'i', 3: nan},
'C': {1: 'i', 2: 'ii', 3: nan}}
I don't really care about performance for my application of this so I haven't bothered checking how efficient this is. Its based on bubblesort so my guess is ~O(N^2).
Maybe this is convoluted, but essentially below works by:
- providing dict_swap_index a nested dictionary and a list. the list should be of the format [i,j,k]. The length should be the depth of the dictionary. Each element corresponds to which position you'd like to move each element to. e.g. [2,0,1] would indicate move element 0 to position 2, element 1 to position 0 and element 2 to position 1.
- this function performs a bubble sort on the order list and dict_, calling deep_swap to swap the levels of the dictionary which are being swapped in the order list
- deep_swap recursively calls itself to find the level provided and returns a dictionary which has been re-ordered
- swap_two_level_dict is called to swap any two levels in a dictionary.
Essentially the idea is to perform a bubble sort on the dictionary, but instead of swapping elements in a list swap levels in a dictionary.
from collections import defaultdict
def dict_swap_index(dict_, order):
for pas_no in range(len(order)-1,0,-1):
for i in range(pas_no):
if order[i] > order[i+1]:
temp = order[i]
order[i] = order[i+1]
order[i+1] = temp
dict_ = deep_swap(dict_, i)
return dict_, order
def deep_swap(dict_, level):
dict_ = deepcopy(dict_)
if level==0:
dict_ = swap_two_level_dict(dict_)
else:
for key in dict_:
dict_[key] = deep_swap(dict_[key], level-1)
return dict_
def swap_two_level_dict(a):
b = defaultdict(dict)
for key1, value1 in a.items():
for key2, value2 in value1.items():
b[key2].update({key1: value2})
return b
e.g.
test_dict = {'a': {'c': {'e':0, 'f':1}, 'd': {'e':2,'f':3}}, 'b': {'c': {'g':4,'h':5}, 'd': {'j':6,'k':7}}}
result = dict_swap_index(test_dict, [2,0,1])
result
(defaultdict(dict,
{'c': defaultdict(dict,
{'e': {'a': 0},
'f': {'a': 1},
'g': {'b': 4},
'h': {'b': 5}}),
'd': defaultdict(dict,
{'e': {'a': 2},
'f': {'a': 3},
'j': {'b': 6},
'k': {'b': 7}})}),
[0, 1, 2])
I want to write a code which takes the following inputs:
list (list of maps)
request_keys (list of strings)
operation (add,substract,multiply,concat)
The code would look at the list for the maps having the same value for all keys except the keys given in request_keys. Upon finding two maps for which the value in the search keys match, the code would do the operation (add,multiple,substract,concat) on the two maps and combine them into one map. This combination map would basically replace the other two maps.
i have written the following peice of code to do this. The code only does add operation. It can be extended to make the other operations
In [83]: list
Out[83]:
[{'a': 2, 'b': 3, 'c': 10},
{'a': 2, 'b': 3, 'c': 3},
{'a': 2, 'b': 4, 'c': 4},
{'a': 2, 'b': 3, 'c': 2},
{'a': 2, 'b': 3, 'c': 3}]
In [84]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:def func(list,request_keys):
: new_list = []
: found_indexes = []
: for i in range(0,len(list)):
: new_item = list[i]
: if i in found_indexes:
: continue
: for j in range(0,len(list)):
: if i != j and {k: v for k,v in list[i].iteritems() if k not in request_keys} == {k: v for k,v in list[j].iteritems() if k not in request_keys}:
: found_indexes.append(j)
: for request_key in request_keys:
: new_item[request_key] += list[j][request_key]
: new_list.append(new_item)
: return new_list
:--
In [85]: func(list,['c'])
Out[85]: [{'a': 2, 'b': 3, 'c': 18}, {'a': 2, 'b': 4, 'c': 4}]
In [86]:
What i want to know is, is there a faster, more memory efficient, cleaner and a more pythonic way of doing the same?
Thank you
You manually generate all the combinations and then compare each of those combinations. This is pretty wasteful. Instead, I suggest grouping the dictionaries in another dictionary by their matching keys, then adding the "same" dictionaries. Also, you forgot the operator parameter.
import collections, operator, functools
def func(lst, request_keys, op=operator.add):
matching_dicts = collections.defaultdict(list)
for d in lst:
key = tuple(sorted(((k, d[k]) for k in d if k not in request_keys)))
matching_dicts[key].append(d)
for group in matching_dicts.values():
merged = dict(group[0])
merged.update({key: functools.reduce(op, (g[key] for g in group))
for key in request_keys})
yield merged
What this does: First, it creates a dictionary, mapping the key-value pairs that have to be equal for two dictionaries to match to all those dictionaries that have those key-value pairs. Then it iterates the dicts from those groups, using one of that group as a prototype and updating it with the sum (or product, or whatever, depending on the operator) of the all the dicts in that group for the required_keys.
Note that this returns a generator. If you want a list, just call it like list(func(...)), or accumulate the merged dicts in a list and return that list.
from itertools import groupby
from operator import itemgetter
def mergeDic(inputData, request_keys):
keys = inputData[0].keys()
comparedKeys = [item for item in keys if item not in request_keys]
grouper = itemgetter(*comparedKeys)
result = []
for key, grp in groupby(sorted(inputData, key = grouper), grouper):
temp_dict = dict(zip(comparedKeys, key))
for request_key in request_keys:
temp_dict[request_key] = sum(item[request_key] for item in grp)
result.append(temp_dict)
return result
inputData = [{'a': 2, 'b': 3, 'c': 10},
{'a': 2, 'b': 3, 'c': 3},
{'a': 2, 'b': 4, 'c': 4},
{'a': 2, 'b': 3, 'c': 2},
{'a': 2, 'b': 3, 'c': 3}]
from pprint import pprint
pprint(mergeDic(inputData,['c']))
I want my function to return multiple dictionaries based on calling arguments. e.g. if i call the function with no argument, it should return all the dictionaries else if i call it with a list of arguments, it should return the corresponding dictionaries from the function. Apparently if i call with no args or one arg, it works fine but i am having problem with using it with multiple values. Following example shows this problem.
def mydef(arg1=None):
a = {'a1':1, 'a2':2}
b = {'b1':1, 'b2':2}
c = {'c1':1, 'c2':2}
d = {'d1':1, 'd2':2}
if arg1 is None:
return a,b,c,d
else:
for x in arg1:
if x == 'a':
return a
elif x == 'b':
return b
w,x,y,z = mydef()
print type(w)
print w
s,t = mydef(['a', 'b'])
print type(s)
print s
print type(t)
print t
Doubt: Lists are returned instead of dicts:
def mydef(args=None):
dicts = { 'a' :{'a1' : 1, 'a2' :2}, 'b' : {'b1' : 1, 'b2' :2}}
if args is None:
args = ['a', 'b']
return [dicts[d] for d in args]
x,y = mydef()
type(x)
>> type 'dict'
type(y)
>> type 'dict'
x = mydef(['a'])
type(x)
>> type 'list'
A function only gets to return once. You can't loop and try to return something on each iteration of the loop; the first one returns and that ends the function. If you want to "return multiple values", what you really have to do is return one value that contains them all, like a list of the values, or a tuple of the values.
Also, it'd be better to put your dictionaries in a dictionary (sup dawg) instead of using local variables to name them. Then you can just pick them out by key.
Here's a way of doing it that returns a list of the selected dicts:
>>> def returnDicts(args=None):
... dicts = {
... 'a': {'a1':1, 'a2':2},
... 'b': {'b1':1, 'b2':2},
... 'c': {'c1':1, 'c2':2},
... 'd': {'d1':1, 'd2':2}
... }
... if args is None:
... args = ['a', 'b', 'c', 'd']
... return [dicts[d] for d in args]
>>> returnDicts()
[{'a1': 1, 'a2': 2},
{'b1': 1, 'b2': 2},
{'c1': 1, 'c2': 2},
{'d1': 1, 'd2': 2}]
>>> returnDicts(['a'])
[{'a1': 1, 'a2': 2}]
>>> returnDicts(['a', 'b'])
[{'a1': 1, 'a2': 2}, {'b1': 1, 'b2': 2}]
why not make them dicts of dicts like so
def mydicts(arg1=None):
dicter = {'a': {'a1':1, 'a2':2},
'b': {'b1':1, 'b2':2},
'c': {'c1':1, 'c2':2},
'd': {'d1':1, 'd2':2},
}
#if arg1 is None return all the dictionaries.
if arg1 is None:
arg1 = ['a', 'b', 'c', 'd']
# Check if arg1 is a list and if not make it one
# Example you pass it a str or int
if not isinstance(arg1, list):
arg1 = [arg1]
return [dicter.get(x, {}) for x in arg1]
Note, this will also return a list of items back to you.