Python_ How to quickly find public key in a dictionary

Source: Internet
Author: User
Tags rounds

Scenario Example:

Spanish football League, scoring stats for each round of players:

First round: {' 1 ': 1, ' 2 ': 4, ' 5 ': 2, ' 7 ': 3}

First round: {' 2 ': 1, ' 5 ': 4, ' 6 ': 2, ' 3 ': 3}

First round: {' 1 ': 1, ' 4 ': 4, ' 6 ': 2, ' 7 ': 3}

......

Question: Count the first n rounds, the players who scored?

How to solve this problem?

Method 1:

#!/usr/bin/python3from random import Randint, sampledef get_s (player):    # get three-season stochastic record    S1 = {K:randint (1, 6) for K In the sample (Player, Randint (3, 6))}    s2 = {K:randint (1, 6) for K in sample (Player, Randint (3, 6))}    s3 = {K:randint ( 1, 6) for K in sample (Player, Randint (3, 6))}    return s1, S2, S3def public_keys (S1, S2, S3):    # define Intermediate list    median = []        # Judging dictionary public key, in S1, at S2, in S3 must be public key for    key in S1:        if key in S2 and key in S3:            median.append (key)    re Turn medianif __name__ = = ' __main__ ':    # generate ABCDEF player    player = ' abcdef '        # get 3 seasons of random number    S1, s2, s3 = get_s ( Player)        # Gets the player who scored for 3 seasons    result = Public_keys (S1,S2,S3)    print (Result)

Method 2:

The player who scored is the key in the data, by finding the keys for each round and then making the intersection to find the results.

#!/usr/bin/python3from random import Randint, sampledef get_s (player):    # get three-season stochastic record    S1 = {K:randint (1, 6) for K In the sample (Player, Randint (3, 6))}    s2 = {K:randint (1, 6) for K in sample (Player, Randint (3, 6))}    s3 = {K:randint ( 1, 6) for K in sample (Player, Randint (3, 6))}    return s1, S2, S3def public_keys (S1, S2, S3):    # Returns the single-sided value of each dictionary and the intersection, which is the result    return S1.keys () & S2.keys () & S3.keys () if __name__ = = ' __main__ ':    # generate ABCDEF player    player = ' abcdef ' 
   # get 3-season random number    S1, s2, s3 = get_s (player)        # get 3 goals for the players scoring player    result = Public_keys (S1, S2, S3)    print ( Result

Method 3:

        When n rounds, through the map and reduce functions

#!/usr/bin/python3from random import Randint, samplefrom functools Import Reducedef get_s (player): # get Random records for three seasons S1 = {K:randint (1, 6) for K in sample (Player, Randint (3, 6))} s2 = {K:ra Ndint (1, 6) for K in sample (Player, Randint (3, 6))} s3 = {K:randint (1, 6) for K in sample (Player, Randint (3, 6))} R  Eturn s1, S2, S3def public_keys (S1, S2, s3): result = reduce (lambda A, b:a & B, Map (Dict.keys, [S1, S2, S3]) # Map (dict. KEYS,[S1,S2,S3,...... N] Get each round of keys set, S1 ...  N for the Dictionary object # Reduce ((Lamda a,b:a&b,map ()) The collection of map obtained, put in a A, B, return the intersection # A, a = A&b, a value taken out of map (Dict.keys, [S1, S2, S3]) get a new list # The lambda in reduce can only pass in two parameters # Map is a higher order function, receive a map (function, [parameter]), iterate over a parameter list, process in an incoming function, return the processing result into a new list return R        Esultif __name__ = = ' __main__ ': # generate abcdef player player = ' abcdef ' # get 3-season random number S1, s2, s3 = get_s (player) # get a player who scored 3 seasons result = Public_keys (S1, S2, S3) Print (result) 

Python_ How to quickly find public key in a dictionary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.