We often have to deal with a variety of data in our work, and it is easy to waste time to write functions when the project is in a rush. There are many built-in functions in Python to help you be more productive!

One: Filter data by criteria in lists and dictionaries

1. Suppose there is a list of numbers data, filter the list of negative numbers in the list derivation

result = [i for i in data if i >= 0]
Copy the code

filter

result = filter(lambda x: x>= 0, data)
Copy the code

2. Students’ math scores will be stored in the form of dictionary, and those whose scores are greater than 80 will be screened

d = {x:randint(50.100) for x in range(1.21)}
Copy the code
{k: v for k, v in d.items() if v > 80}
Copy the code

Two: the dictionary key-value pair is flipped

Use the zip() function

The zip() function takes an iterable object as an argument, packs the corresponding elements of the object into tuples, and returns a list of those tuples.

>>> s1 = {x: randint(1.4) for x in sample('abfcdrg', randint(1.5))}
>>> s1
{'b': 1.'f': 4.'g': 3.'r': 1}
>>> d = {k:v for k, v in zip(s1.values(), s1.keys())}
>>> d
{1: 'r'.4: 'f'.3: 'g'}
Copy the code

2. Frequency of occurrence of elements in statistical sequence

1. In a random sequence, find the three elements with the highest frequency of occurrence. How many times do they occur?

The random sequence is as follows:

data = [randint(0.20) for _ in range(20)]
Copy the code

Method 1: You can use a dictionary for statistics, using the data in the list as the key and the number of occurrences as the value

from random import randint

def demo(a):
    data = [randint(0.20) for _ in range(30)]
    # Number of occurrences of a number in a list
    d = dict.fromkeys(data, 0)
    for v in li:
        d[v] += 1
    return d
Copy the code

Method 2: Use the Counter object directly under the Collections module

>>> data = [randint(0.20) for _ in range(30)]
>>> data
[7.8.5.16.10.16.8.17.11.18.11.17.15.7.2.19.5.16.17.17.12.19.9.10.0.20.11.2.11.10]
>>> c2 = Counter(data)
>>> c2
Counter({17: 4.11: 4.16: 3.10: 3.7: 2.8: 2.5: 2.2: 2.19: 2.18: 1.15: 1.12: 1.9: 1.0: 1.20: 1})
>>> c2[14]
4
>>> c2.most_common(3)  # 3 numbers with the highest statistical frequency
[(17.4), (11.4), (16.3)]
Copy the code

2. Make statistics of words in an English article and find the words with the highest frequency and the frequency of occurrence

From the exercise above, we know that we can solve it with Counter

import re
from collections import Counter

# Count the frequency of words in English and Chinese of an article

with open('test.txt'.'r', encoding='utf-8')as f:
    d = f.read()

total = re.split('\W+', d)  # List of all words
result = Counter(total)
print(result.most_common(10))
Copy the code

Sort the items in the dictionary according to the size of the values in the dictionary

For example, the math scores of students in the class are stored in the form of dictionaries:

{"Lnad": 88."Jim".71...}
Copy the code

Please sort by math score from highest to lowest!

Method 1: Use ZIP to transform the dictionary into a meta-ancestor, and then use sorted

>>> data = {x: randint(60.100) for x in "xyzfafs"}
>>> data
{'x': 73.'y': 69.'z': 76.'f': 61.'a': 64.'s': 100}
>>> sorted(data)
['a'.'f'.'s'.'x'.'y'.'z']
>>> data = sorted(zip(data.values(), data.keys()))
>>> data
[(61.'f'), (64.'a'), (69.'y'), (73.'x'), (76.'z'), (100.'s')]
Copy the code

Method 2: Use the sorted function’s key argument

>>> data.items()
>>> dict_items([('x'.64), ('y'.74), ('z'.66), ('f'.62), ('a'.80), ('s'.72)])
>>> sorted(data.items(), key=lambda x: x[1[(])'f'.62), ('x'.64), ('z'.66), ('s'.72), ('y'.74), ('a'.80)]
Copy the code

Find common keys in multiple dictionaries

Practical scenario: In a football league, players who score goals in each round are counted

First round: {” Ronaldo “: 1, “Suarez “:2,” Torres “: 1.. } Second round: {” Neymar “: 1, “Messi “:2,” Mbappe “: 3.. } third round: {” mbappe “:2, “Ronaldo “:2,” Neymar “: 1.. }

Simulation of random goalscoring players and goals scored

>>> s1 = {x: randint(1.4) for x in sample('abfcdrg', randint(1.5))}
>>> s1
{'d': 3.'g': 2}
>>> s2 = {x: randint(1.4) for x in sample('abfcdrg', randint(1.5))}
>>> s2
{'b': 4.'g': 1.'f': 1.'r': 4.'d': 3}
>>> s3 = {x: randint(1.4) for x in sample('abfcdrg', randint(1.5))}
>>> s3
{'b': 4.'r': 4.'a': 2.'g': 3.'c': 4}
Copy the code

Obtain the keys of the dictionary first, and then take the intersection of the keys of each round. Since the number of rounds is variable, map is used for batch operation

map(dict.keys, [s1, s2, s3])
Copy the code

Then accumulate the intersection and use the reduce function

reduce(lambda x,y: x & y, map(dict.keys, [s1, s2, s3]))
Copy the code

One line of code!