I receive a dictionary as input, and want to return a list of keys for which the dictionary values are unique in the scope of that dictionary.
I will clarify with an example. Say my input is dictionary a, constructed as follows:
a = dict()
a['cat'] = 1
a['fish'] = 1
a['dog'] = 2 # <-- unique
a['bat'] = 3
a['aardvark'] = 3
a['snake'] = 4 # <-- unique
a['wallaby'] = 5
a['badger'] = 5
The result I expect is ['dog', 'snake'].
There are obvious brute force ways to achieve this, however I wondered if there's a neat Pythonian way to get the job done.
I think efficient way if dict is too large would be
countMap = {}
for v in a.itervalues():
countMap[v] = countMap.get(v,0) + 1
uni = [ k for k, v in a.iteritems() if countMap[v] == 1]
Here is a solution that only requires traversing the dict once:
def unique_values(d):
seen = {} # dict (value, key)
result = set() # keys with unique values
for k,v in d.iteritems():
if v in seen:
result.discard(seen[v])
else:
seen[v] = k
result.add(k)
return list(result)
Note that this actually is a bruteforce:
l = a.values()
b = [x for x in a if l.count(a[x]) == 1]
>>> b = []
>>> import collections
>>> bag = collections.defaultdict(lambda: 0)
>>> for v in a.itervalues():
... bag[v] += 1
...
>>> b = [k for (k, v) in a.iteritems() if bag[v] == 1]
>>> b.sort() # optional
>>> print b
['dog', 'snake']
>>>
A little more verbose, but does need only one pass over a:
revDict = {}
for k, v in a.iteritems():
if v in revDict:
revDict[v] = None
else:
revDict[v] = k
[ x for x in revDict.itervalues() if x != None ]
( I hope it works, since I can't test it here )
What about subclassing?
class UniqueValuesDict(dict):
def __init__(self, *args):
dict.__init__(self, *args)
self._inverse = {}
def __setitem__(self, key, value):
if value in self.values():
if value in self._inverse:
del self._inverse[value]
else:
self._inverse[value] = key
dict.__setitem__(self, key, value)
def unique_values(self):
return self._inverse.values()
a = UniqueValuesDict()
a['cat'] = 1
a['fish'] = 1
a[None] = 1
a['duck'] = 1
a['dog'] = 2 # <-- unique
a['bat'] = 3
a['aardvark'] = 3
a['snake'] = 4 # <-- unique
a['wallaby'] = 5
a['badger'] = 5
assert a.unique_values() == ['dog', 'snake']
Here's another variation.
>>> import collections
>>> inverse= collections.defaultdict(list)
>>> for k,v in a.items():
... inverse[v].append(k)
...
>>> [ v[0] for v in inverse.values() if len(v) == 1 ]
['dog', 'snake']
I'm partial to this because the inverted dictionary is such a common design pattern.
You could do something like this (just count the number of occurrences for each value):
def unique(a):
from collections import defaultdict
count = defaultdict(lambda: 0)
for k, v in a.iteritems():
count[v] += 1
for v, c in count.iteritems():
if c <= 1:
yield v
Use nested list comprehensions!
print [v[0] for v in
dict([(v, [k for k in a.keys() if a[k] == v])
for v in set(a.values())]).values()
if len(v) == 1]
Related
I'm trying to rewrite the following code
dct = {}
x = 'x'
y = 'y'
z = 'z'
if x not in dct:
dct[x] = defaultdict(dict)
if y not in dct[x]:
dct[x][y] = defaultdict(dict)
if z not in dct[x][y]:
dct[x][y][z] = defaultdict(list)
dct[x][y][z]['b'].append(defaultdict(int))
dct[x][y][z]['b'][0]['g']+=1
Without the following lines:
if x not in dct:
dct[x] = defaultdict(dict)
if y not in dct[x]:
dct[x][y] = defaultdict(dict)
if z not in dct[x][y]:
dct[x][y][z] = defaultdict(list)
dct[x][y][z]['b'].append(defaultdict(int))
Ideally I would like to have syntax such as
dct = 'state what it is'
dct[x][y][z]['b'][0]['g']+=1
I wrote a customised implementation of defaultdict which can do this to an arbitrary depth, unlike the fixed depth from previous answers.
class DefaultDict(dict):
def __missing__(self, name):
rval=type(self)()
self.__setitem__(name, rval)
return rval
Here's some example usage:
>>> dct=DefaultDict()
>>> dct[0][1]['bees'][('any','hashable','object')]=2
>>> dct[0][1]['bees'][('any','hashable','object')]
2
>>> 0 in dct
True
>>> 1 in dct
False
>>> dct
{0: {1: {'bees': {('any', 'hashable', 'object'): 2}}}}
Use lambdas.
from collections import defaultdict as dd
dct = dd(lambda: dd(lambda: dd(int)))
dct["foo"][1][("a", 7)] += 1
I have two dictionaries K and L. K is a Dictionary of lists and L is a nested dictionary
K={
'k1':[1,3,2],
'k2':[3,4,5],
'k3':[9,10,11]
}
L = {
'l1':{'a':'1','b':'1','c':'2'},
'l2':{'a':'1','b':'3','c':'2'},
'l3':{'a':'1','b':'2','c':'2'},
'l4':{'a':'1','b':'4','c':'2'}
}
What I need:
I want to create a new dictionary of lists based on the below condition.
I need to check if b values of L are present in the K dictionary of the list or not.
If present I need to return a new dictionary with Key of K
dictionary and Key of L dictionary.
For the above example, I need to return something like this
M = {'k1':[l1,l3,l2]
'k2':[l2,l4]
}
Here is the code that I have tried:
M= {}
for k, v in K.items():
temp = []
for i in v:
if L[i]['b'] in K[k]:
temp.append(i)
M[k] = temp
print(M)
Here is your solution:
new_dict, M = dict(), dict()
for key, val in L.items():
new_dict[val['b']] = key
for key, val in K.items():
for x in val:
if str(x) in new_dict:
tmp = new_dict[str(x)]
lst = M[key] if key in M else list()
lst.append(tmp)
M[key] = lst
print(M)
For the following K, L:
K = {
'k1':[1,3,2],
'k2':[3,4,5],
'k3':[9,10,11]
}
L = {
'l1':{'a':'1','b':'1','c':'2'},
'l2':{'a':'1','b':'3','c':'2'},
'l3':{'a':'1','b':'2','c':'2'},
'l4':{'a':'1','b':'4','c':'2'}
}
Output M:
M = {
'k2': ['l2', 'l4'],
'k1': ['l1', 'l2', 'l3']
}
[ Considered b values of L has a unique value, if not then you have to store it as a list in the new_dict dictionary.]
You were looking up the wrong thing in L here L[I]. I would do this by creating a lookup intially from L, that way you don't have to loop through L for every key in K. From there you can go through K once, looking up the items from the lookup.
Using defaultdict also allows you to handle the case where L has b values that are the same
from collections import defaultdict
b_val_to_l = defaultdict(set)
for lkey, ldict in L.items():
# For each ldict, extract the 'b' value, convert to an int and add the
# l key to the set of l values for the corresponding b value
b_val_to_l[int(ldict['b'])].add(lkey)
print(b_val_to_l)
M = defaultdict(list)
for k, v in K.items():
for i in v:
# For each value in the k, get the set of corresponding l values
b_set = b_val_to_l.get(i, set())
# Add to the output dict
M[k].extend(list(b_set))
print(M)
Just do this:
Mtemp = { k: [l for l in L
if int(L[l]['b']) in K[k]
] for k in K }
# Mtemp can have None values, so let's remove them...
M = { k: l for k, l in Mtemp.items() if len(l) > 0 }
Output:
{'k1': ['l1', 'l2', 'l3'], 'k2': ['l2', 'l4']}
I have two lists, one is of form:
A = ["qww","ewq","ert","ask"]
B = [("qww",2) ,("ert",4) , ("qww",6), ("ewq" , 5),("ewq" , 10),("ewq" , 15),("ask",11)]
I have to process such that final output is
C = A = [("qww",8),("ewq",20),("ert",4),("ask",11)]
for that I written code:
# in code temp_list is of form A
# in code total_list is of form B
# in code final is of form C
def ispresent(key,list):
for qwerty in list:
if qwerty == key:
return 1
else:
return 0
def indexreturn(key,list):
counter = 0
for qwerty in list:
if qwerty != key:
counter = counter + 1
else:
return counter
def mult_indexreturn(key,list):
for i in range(len(list)):
if key == list[i][0]:
return i
final = map(lambda n1, n2: (n1,n2 ), temp_list,[ 0 for _ in range(len(temp_list))])
for object2 in total_list:#****
for object1 in temp_list:
if object2 == object1:
final[ indexreturn(object2,final) ][1] = final[ indexreturn(object2, final) ][1] + object2[mult_indexreturn(object2,total_list)][1]#total_list[ mult_indexreturn(object2,total_list) ][1]
print(final)
it should give output as C type list, but giving nothing
but C = [("qww",0),("ewq",0),("ert",0),("ask",0)]
according to me the main problem is in my looping part ( with **** comment), is there problem with logic or something else.
I gave in lot of codes, so that you can understand how my code working
You can build a dictionary using the method fromkeys() and subsequently you can use the for loop to accumulate integers:
A = ["qww","ewq","ert","ask"]
B = [("qww",2) ,("ert",4) , ("qww",6), ("ewq" , 5),("ewq" , 10),("ewq" , 15),("ask",11)]
C = dict.fromkeys(A, 0)
# {'qww': 0, 'ewq': 0, 'ert': 0, 'ask': 0}
for k, v in B:
C[k] += v
C = list(C.items())
# [('qww', 8), ('ewq', 30), ('ert', 4), ('ask', 11)]
Try this:
from collections import defaultdict
result = defaultdict(int)
for i in A:
result[i] = sum([j[1] for j in B if j[0] == i])
then tuple(result.items()) will be your out put.
Or you can do it in just one line:
result = tuple({i:sum([j[1] for j in B if j[0] == i]) for i in A}.items())
Using collection.defaultdict
Ex:
from collections import defaultdict
A = ["qww","ewq","ert","ask"]
B = [("qww",2) ,("ert",4) , ("qww",6), ("ewq" , 5),("ewq" , 10),("ewq" , 15),("ask",11)]
result = defaultdict(int)
for key, value in B:
if key in A: #Check if Key in A.
result[key] += value #Add Value.
print(result)
Output:
defaultdict(<type 'int'>, {'qww': 8, 'ert': 4, 'ewq': 30, 'ask': 11})
How can I make a directory have values count from a list that is associated with the Keys, opposed to the occurrences of a particular Key.
EXAMPLE: I have two lists, One is of strings, and one is of a values for each string.
say:
strings = ['me', 'you', 'me', 'her','her']
values = [1,20,6,35,5]
and I want:
directory = {'me':7, 'you':20, 'her':40}
This is how I have been making directories:
d = {}
for i in list:
if i in d:
d[i] = d[i]+1
else:
d[i] = 1
valueKeys = zip(d.values(),d.keys())
value = d.values()
keys = d.keys()
>>> from collections import defaultdict
>>> d = defaultdict(int)
>>> for x, y in zip(strings, values):
... d[x] += y
...
>>> d
defaultdict(<class 'int'>, {'you': 20, 'her': 40, 'me': 7})
The resulted defaultdict object can act as plain dict:
for key in d:
print(key, d[key])
Output:
me 7
her 40
you 20
#x=numbers
#list = names
d = {}
for i in list:
if i in d:
d[i] = d[i]+x[list.index(i)]
else:
d[i] = x[list.index(i)]
valueKeys = zip(d.values(),d.keys())
value = d.values()
keys = d.keys()
This was how I solved it
Given a list of dictionaries ( each of which have same keys), I want total number of different values with which a given key is associated
$ li = [{1:2,2:3},{1:2,2:4}] $ the expected output is {1:1,2:2}
I came up with the following piece of code...Is there a better way of doing this ?
counts = {}
values = {}
for i in li:
for key,item in i.items():
try:
if item in values[key]:
continue
except KeyError:
else:
try:
counts[key] += 1
except KeyError:
counts[key] = 1
try:
values[key].append(item)
except KeyError:
values[key] = [item]
Something like this is probably more direct:
from collections import defaultdict
counts = defaultdict(set)
for mydict in li:
for k, v in mydict.items():
counts[k].add(v)
That takes care of the collecting / counting of the values. To display them like you want them, this would get you there:
print dict((k, len(v)) for k, v in counts.items())
# prints {1: 1, 2: 2}
Here is yet another alternative:
from collections import defaultdict
counts = defaultdict(int)
for k, v in set(pair for d in li for pair in d.items()):
counts[k] += 1
And the result:
>>> counts
defaultdict(<type 'int'>, {1: 1, 2: 2})
You could so something like this:
li = [{1:2,2:3},{1:2,2:4}]
def makesets(x, y):
for k, v in x.iteritems():
v.add(y[k])
return x
distinctValues = reduce(makesets, li, dict((k, set()) for k in li[0].keys()))
counts = dict((k, len(v)) for k, v in distinctValues.iteritems())
print counts
When I run this it prints:
{1: 1, 2: 2}
which is the desired result.
counts = {}
values = {}
for i in li:
for key,item in i.items():
if not (key in values.keys()):
values[key] = set()
values[key].add(item)
for key in values.keys():
counts[key] = len(values[key])
using flattening list in case dicts are not alway same length:
li=[{1: 2, 2: 3}, {1: 2, 2: 4}, {1: 3}]
dic={}
for i,j in [item for sublist in li for item in sublist.items()]:
dic[i] = dic[i]+1 if i in dic else 1