Python 3 - intersection of multiple dictionary key - python

below is my code, any one can help me to optimize the process by using python 3 build-in library function ?
Dict1 = {'ky1':1, 'ky2':2,'ky_3':3}
Dict2 = {'ky1':4, 'ky2':5,'ky_4':6}
Dict3 = {'ky2':7, 'ky3':8,'ky_5':9}
D = [Dict1,Dict2,Dict3]
Keys_list = []
for i in D:
tmp = list(i.keys())
Keys_list.append(tmp)
Output = list(set.intersection(*map(set,Keys_list)))
My Dict1, Dict2, Dict3 is large dictionary
thanks

If you just want the list of all keys in all of the dictionaries, you can use dict.viewkeys() (For Python 2.7) or dict.keys() in Python 3.x , to get the dictionary view object, and then intersect them.
Example for Python 3.x -
>>> Dict1 = {'ky1':1, 'ky2':2,'ky_3':3}
>>> Dict2 = {'ky1':4, 'ky2':5,'ky_4':6}
>>> Dict3 = {'ky2':7, 'ky3':8,'ky_5':9}
>>>
>>> Dict1.keys() & Dict2.keys() & Dict3.keys()
{'ky2'}
>>> list(Dict1.keys() & Dict2.keys() & Dict3.keys())
['ky2']
For Python 2.7 use Dict1.viewkeys() , etc, instead of .keys() .
If you have a list of dictionaries , one way to do this in one line using functools.reduce() function, would be -
>>> ld = [{'ky1':1, 'ky2':2,'ky_3':3},{'ky1':4, 'ky2':5,'ky_4':6},{'ky2':7, 'ky3':8,'ky_5':9}]
>>> res = list(reduce(lambda x, y: x & y.keys() , ld))
>>> res
['ky2']
Similar logic, using for loop -
>>> ld = [{'ky1':1, 'ky2':2,'ky_3':3},{'ky1':4, 'ky2':5,'ky_4':6},{'ky2':7, 'ky3':8,'ky_5':9}]
>>> res = ld.pop()
>>> for d in ld:
... res = res & d.keys()
...
>>> list(res)
['ky2']

If I understood your question properly, you're looking for the intersecting keys amongst all three dictionaries, right?
If that is the case, you only need to iterate over one of them, and the process is simple.
isect = [i for i in Dict1 if all(i in d for d in (Dict1,Dict2,Dict3))]
print(isect)
The list comprehension iterates over one dict, and each item is looked up in all three dicts. If it is present, it will be added to the list. You'll get:
['ky2']
as the output.

Related

How to refactor my for loop as python dictionary comprehension?

I am using Python 3.6. I wrote following code, it's a for loop:
Edit: I made a mistake when I wrote var l down, so I re-type it here. Thanks for #Ender Look !!
l = [['a','1'], ['a','2'], ['a','3']]
d = {}
for i in l:
d[i[0]] = d.get(i[0], '') + '\t' + i[1]
print (d)
So, the result is what I want:
{'a': '\t1\t2\t3'}
Then I refactor above code as comprehension:
dict2 = {}
dict2 = {i[0]: dict2.get(i[0], '') + '\t' + i[1] for i in l}
print(dict2)
I though they should return same output. But the dict2 is:
{'a': '\t3'}
I want to know what's the matter with my dict comprehension? Thanks a lot!
You don't necessarily need to use a comprehension here. You can make use of defaultdict from collections:
>>> from collections import defaultdict
>>> d = defaultdict(str)
>>> for li in l:
... d[li[0]] += f'\t{li[1]}'
...
>>> d
defaultdict(<class 'str'>, {'a': '\t1\t2'})
Your dictionary comprehension doesn't work because it's use .get on itself.
dict2 = {i[0]: dict2.get(i[0], '') + '\t' + i[1] for i in l}
Until the end of the whole dictionary comprehension, this new dictionary isn't assigned to your actual dict2 variable. So all the times your comprehension tries to retrieve the value from dict2.get(...) it always executes that function from the empty dictionary dict2 = {}.
Sadly, I don't know (and I don't think it exists) a way to use .get on a dictionary comprehension about itself, because the variable dict2 isn't updated on "real time" (it wait until the end the comprehension). Or at least that I have understood, my humble knowledge isn't perfect.
Yes, you can do it as a single dict comprehension.
{k: v
for d in [{}]
for k, v in [d.__setitem__(k, d.get(k, '') + '\t' + v) or d
for k, v in [['a','1'], ['a','2'], ['a','3']]][-1].items()}
You shouldn't.
Note how we had to create a reference to an inner dict d anyway, since your algorithm has to look things up in the dict as it's being constructed. What is the outer comprehension even for? And we're throwing away all but the last element of the inner list comp.
It's much clearer to use a normal for loop like this,
d = {}
for k, v in [['a','1'], ['a','2'], ['a','3']]:
d[k] = d.get(k, '') + '\t' + v
Use the right tool for the job.

If dict2 value = dict1 key, replace entire dict2 value with dict1 value

I have two dictionaries. In both dictionaries, the value of each key is a single list. If any element in any list in dictionary 2 is equal to a key of dictionary 1, I want to replace that element with the first element in that dictionary 1 list.
In other words, I have:
dict1 = {'IDa':['newA', 'x'], 'IDb':['newB', 'x']}
dict2 = {1:['IDa', 'IDb']}
and I want:
dict2 = {1:['newA', 'newB']}
I tried:
for ID1, news in dict1.items():
for x, ID2s in dict2.items():
for ID in ID2s:
if ID == ID1:
print ID1, 'match'
ID.replace(ID, news[0])
for k, v in dict2.items():
print k, v
and I got:
IDb match
IDa match
1 ['IDa', IDb']
So it looks like everything up to the replace method is working. Is there a way to make this work? To replace an entire string in a value-list with a string in another value-list?
Thanks a lot for your help.
Try this:
dict1 = {'IDa':['newA', 'x'], 'IDb':['newB', 'x']}
dict2 = {1:['IDa', 'IDb']}
for key in dict2.keys():
dict2[key] = [dict1[x][0] if x in dict1.keys() else x for x in dict2[key]]
print dict2
this will print:
{1: ['newA', 'newB']}
as required.
Explanation
dict.keys() gives us just the keys of a dictionary (i.e. just the left hand side of the colon). When we use for key in dict2.keys(), at present our only key is 1. If the dictionary was larger, it'd loop through all keys.
The following line uses a list comprehension - we know that dict2[key] gives us a list (the right side of the colon), so we loop through every element of the list (for x in dict2[key]) and return the first entry of the corresponding list in dict1 only if we can find the element in the keys of dict1 (dict1[x][0] if x in dict1.keys) and otherwise leave the element untouched ([else x]).
For example, if we changed our dictionaries to be the following:
dict1 = {'IDa':['newA', 'x'], 'IDb':['newB', 'x']}
dict2 = {1:['IDa', 'IDb'], 2:{'IDb', 'IDc'}}
we'd get the output:
{1: ['newA', 'newB'], 2: ['newB', 'IDc']}
because 'IDc' doesn't exist in the keys of dict1.
You could also use dictionary comprehensions, but I am not sure that they are working in Python 2.7, it may be limited to Python 3 :
# Python 3
dict2 = {k: [dict1.get(e, [e])[0] for e in v] for k,v in dict2.items()}
edit: I just checked, this is working in Python 2.7. However, dict2.items() should be replaced by dict2.iteritems() :
# Python 2.7
dict2 = {k: [dict1.get(e, [e])[0] for e in v] for k,v in dict2.iteritems()}
This was a fun one!
dict2[1] = [dict1[val][0] if val in dict1 else val for val in dict2[1]]
Or, here is the same logic without list comprehension:
new_dict = {1: []}
for val in dict2[1]:
if val in dict1:
new_dict[1].append(dict1[val][0])
else:
new_dict[1].append(val)
dict2 = new_dict

assign values to list of variables in python

I have made a small demo of a more complex problem
def f(a):
return tuple([x for x in range(a)])
d = {}
[d['1'],d['2']] = f(2)
print d
# {'1': 0, '2': 1}
# Works
Now suppose the keys are programmatically generated
How do i achieve the same thing for this case?
n = 10
l = [x for x in range(n)]
[d[x] for x in l] = f(n)
print d
# SyntaxError: can't assign to list comprehension
You can't, it's a syntactical feature of the assignment statement. If you do something dynamic, it'll use different syntax, and thus not work.
If you have some function results f() and a list of keys keys, you can use zip to create an iterable of keys and results, and loop over them:
d = {}
for key, value in zip(keys, f()):
d[key] = value
That is easily rewritten as a dict comprehension:
d = {key: value for key, value in zip(keys, f())}
Or, in this specific case as mentioned by #JonClements, even as
d = dict(zip(keys, f()))

Selecting elements of a Python dictionary greater than a certain value

I need to select elements of a dictionary of a certain value or greater. I am aware of how to do this with lists, Return list of items in list greater than some value.
But I am not sure how to translate that into something functional for a dictionary. I managed to get the tags that correspond (I think) to values greater than or equal to a number, but using the following gives only the tags:
[i for i in dict if dict.values() >= x]
.items() will return (key, value) pairs that you can use to reconstruct a filtered dict using a list comprehension that is feed into the dict() constructor, that will accept an iterable of (key, value) tuples aka. our list comprehension:
>>> d = dict(a=1, b=10, c=30, d=2)
>>> d
{'a': 1, 'c': 30, 'b': 10, 'd': 2}
>>> d = dict((k, v) for k, v in d.items() if v >= 10)
>>> d
{'c': 30, 'b': 10}
If you don't care about running your code on python older than version 2.7, see #opatut answer using "dict comprehensions":
{k:v for (k,v) in dict.items() if v > something}
While nmaier's solution would have been my way to go, notice that since python 2.7+ there has been a "dict comprehension" syntax:
{k:v for (k,v) in dict.items() if v > something}
Found here: Create a dictionary with list comprehension in Python. I found this by googling "python dictionary list comprehension", top post.
Explanation
{ .... } includes the dict comprehension
k:v what elements to add to the dict
for (k,v) in dict.items() this iterates over all tuples (key-value-pairs) of the dict
if v > something a condition that has to apply on every value that is to be included
You want dict[i] not dict.values(). dict.values() will return the whole list of values that are in the dictionary.
dict = {2:5, 6:2}
x = 4
print [dict[i] for i in dict if dict[i] >= x] # prints [5]

Refactoring with python dictionary comprehension

I have 2 dictionary which contain the same keys but the value pairs are different. Let's make dictA and dictB represent the two dictionaries in question.
dictA = {'key1':'Joe', 'key2':'Bob'}
dictB = {'key1':'Smith', 'key2':'Johnson'}
Currently, I am creating a new dictionary based the common occurring keys through a nested if statement. In doing so, the values that share a key are contained within a list, in the new dictionary. See this done below:
dictAB = {} # Create a new dictionary
# Create a list container for dictionary values
for key in dictA.keys():
dictAB[key] = []
# Iterate through keys in both dictionaries
# Find matching keys and append the respective values to the list container
for key, value in dictA.iteritems():
for key2, value2 in dictB.iteritems():
if key == key2:
dictAB[key].append(value)
dictAB[key].append(value2)
else:
pass
How can this be made into a more clean structure using python dictionary comprehension?
Use sets or key views (python 2.7):
dictAB = {k: [dictA[k], dictB[k]] for k in dictA.viewkeys() & dictB.viewkeys()}
Before 2.7:
dictAB = dict((k, [dictA[k], dictB[k]]) for k in set(dictA) & set(dictB))
In python 3, you can use the .keys method for such operations directly, as they are implemented as views:
dictAB = {k: [dictA[k], dictB[k]] for k in dictA.keys() & dictB.keys()}
Demo (python 2.7):
>>> dictA = {'key1':'Joe', 'key2':'Bob'}
>>> dictB = {'key1':'Smith', 'key2':'Johnson'}
>>> dictAB = {k: [dictA[k], dictB[k]] for k in dictA.viewkeys() & dictB.viewkeys()}
>>> print dictAB
{'key2': ['Bob', 'Johnson'], 'key1': ['Joe', 'Smith']}
The & operator on either two sets or on a dict view creates the intersection of both sets; all keys that are present in both sets.
By using an intersection of the keys, this code will work even if either dictA or dictB has keys that do not appear in the other dictionary. If you are absolutely sure the keys will always match, you could just iterate over either dict directly without the intersection:
dictAB = {k: [dictA[k], dictB[k]] for k in dictA}
dictAB = { key: [dictA[key],dictB[key]] for key in dictA if key in dictB }

Categories

Resources