How does Python find multiple dictionaries public key key
Today, I would like to share with you how Python finds the relevant knowledge points of multiple dictionary public keys key. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
1. How to quickly find common keys in multiple dictionaries (key)
Actual case:
Spanish Football League A, statistics of players' goals in each round:
The first round: {Suarez: 1, Messi: 2, Benzema: 1, Cristiano Ronaldo: 3,.}
Second round: {'Suarez': 2, 'Ronaldo': 1, 'Grizman': 2, 'Bell': 1,.}
The third round: {Suarez: 1, Torres: 2, Bell: 1, Neymar: 1,.}
Statistics show that in the first N rounds, there are players who score goals in every game.
Note: public keys are the keys that appear in every dictionary.
Solution:
Using the intersection operation of set (set)
Step1: use the dictionary's keys () method to get a collection of dictionary keys
Step2: use the map function to get a collection of keys for all dictionaries
Step3: use the reduce function to take the intersection of the keys sets of all dictionaries.
2. Code demonstration from random import randint, sample # Random sampling goal player information print (sample ('abcdef', randint (3,6) # Dictionary parsing produces each round data S1 = {x: randint (1,4) for x in sample (' abcdef', randint (3,6))} S2 = {x: randint (1,4) for x in sample ('abcdef', randint (3,6))} S3 = {x: randint (1,4) for x in sample (' abcdef', randint (3)) 6))} print (S1, S2, S3) # method 1-iterative first round middle key Then determine whether the key is in S2 and S3. If it is, it means that the key is the public key res = [] for k in S1: if k in S2 and k in S3: res.append (k) print (res) # method 2if k in # get all the key,python2 in each dictionary as viewkeys () print (s1.keys (), s2.keys (), s3.keys ()) # take the intersection of all sets Is the common key print (s1.keys () & s2.keys () & s3.keys ()) # method 3 map to get each round of keys set print (list (dict.keys (dict.keys, [S1, S2, S3])) from functools import reduce# uses the reduce function Take the intersection print of each round of keys sets (reduce (lambda a, b: a & b, map (dict.keys, [S1, S2, S3])) 3. Method 1: for in cycle from random import randint, samplea1 = {k: randint (1,4) for k in 'abcdefg'} a2 = {k: randint (1,4) for k in' abc123456789'} a3 = {k: randint (1) 4) for k in 'abcinubububu'} a4 = {k: randint (1,4) for k in' abc89898989'} r = [] for x in A1: if x in a2 and x in a3 and x in a4: r.append (x) print (r) randint (1,4) # method 2: use the intersection operation from random import randint, samplea1 = {k: randint (1,4) for k in 'abcdefg'} a2 = {k: randint (1) 4) for k in 'abcdefg'} a3 = {k: randint (1,4) for k in' abcdefg'} a4 = {k: randint (1,4) for k in 'abcdefg'} a = a1.keys () & a2.keys () & a3.keys () & a4.keys () print (a)
A1.keys (): get the key of A1 dictionary, a set format
A1.keys () & a2.keys () & a3.keys () & a4.keys (): take the common elements of four sets
An is a set (set)
Method 3: use map, namely reduce (public key for n dictionaries) from random import randint, samplefrom functools import reducea1 = {k: randint (1,4) for k in 'abcdefg'} a2 = {k: randint (1,4) for k in' abcdefg'} a3 = {k: randint (1,4) for k in 'abcdefg'} a4 = {k: randint (1,4) for k in' abcdefg'} b1 = map (dict.keys, [A1, a2, a3, a4]) b2 = reduce (lambda a) B: a & b, b1) print (b2) b1 = map (dict.keys, [A1, a2, A3, a4]) # take the keys of each dictionary in the form of a collection This is all the content of the article "how to find multiple Dictionary Public Keys key by Python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.