Github has 4600 + stars. Learn a Python trick every 30 seconds. Love it

Posted by universelittle on Sat, 12 Feb 2022 02:58:57 +0100

Many friends who study Python will encounter many problems in the implementation of functions in the actual project. Some problems are not very difficult, or there are good methods to solve them. Of course, who can make a coincidence? When we are proficient in the code, we can naturally summarize some useful skills, but it may not be so easy for those who are just familiar with Python.

This time, we recommend a good resource "30 seconds of Python" to learn these skills. All skills and methods can be obtained in 30 seconds, which can be accumulated by using business time. Let's take a quick look.

Python button skirt: 797403929

1. Contents

The following is the whole directory of learning Python in 30 seconds, which is divided into several sections: List, Math, Object, String and Utility. The following is the sorted mind map.

I have selected 10 practical and interesting methods to share with you, and others who are interested can learn by themselves.

1. List: all_equal

Function realization: check whether all elements in a list are the same.
Interpretation: use [1:] and [: - 1] to compare all elements of a given list.

def all_equal(lst):
 return lst[1:] == lst[:-1]

give an example:

all_equal([1, 2, 3, 4, 5, 6]) # False
all_equal([1, 1, 1, 1]) # True

2. List: all_unique

Function implementation: if all values in the list are unique, return True; otherwise, False
Interpretation: use set() to remove duplicates on a given list and compare its length with the original list.

def all_unique(lst):
 return len(lst) == len(set(lst))

give an example:

x = [1,2,3,4,5,6]
y = [1,2,2,3,4,5]
all_unique(x) # True
all_unique(y) # False

3. List: bifurcate

Function realization: group the list values. If the element in the filter is True, the corresponding element belongs to the first group; Otherwise, it belongs to the second group.
Interpretation: use list derivation and enumerate() to filter elements to each group.

def bifurcate(lst, filter):
 return [
   [x for i,x in enumerate(lst) if filter[i] == True],
   [x for i,x in enumerate(lst) if filter[i] == False]

give an example:

bifurcate(['beep', 'boop', 'foo', 'bar'], [True, True, False, True])
# [ ['beep', 'boop', 'bar'], ['foo'] ]

4. List: difference

Function realization: return the difference between two iterables.
Interpretation: create a set of b, and the list derivation of a is not retained_ Elements in b.

def difference(a, b):
 _b = set(b)
 return [item for item in a if item not in _b]

give an example:

difference([1, 2, 3], [1, 2, 4]) # [3]

5. List: flatten

Function realization: one-time integration list.
Interpretation: use nested lists to extract each value of sub lists.

def flatten(lst):
 return [x for y in lst for x in y]

give an example:

flatten([[1,2,3,4],[5,6,7,8]]) # [1, 2, 3, 4, 5, 6, 7, 8]

6. Math: digitize

Function realization: decompose and convert a number into a single digit number.
Interpretation: after n is characterized, use map() function combined with int to complete the conversion

def digitize(n):
 return list(map(int, str(n)))

give an example:

digitize(123) # [1, 2, 3]

7. List: shuffle

Function realization: randomly disrupt the order of list elements.
Interpretation: reorder list elements using Fisher Yates algorithm.

from copy import deepcopy
from random import randint

def shuffle(lst):
 temp_lst = deepcopy(lst)
 m = len(temp_lst)
 while (m):
   m -= 1
   i = randint(0, m)
   temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m]
 return temp_lst

give an example:

foo = [1,2,3]
shuffle(foo) # [2,3,1] , foo = [1,2,3]

8. Math: clamp_number

Function realization: clamp the digital num in the range specified by the boundary values of a and b.
Interpretation: if num falls within the range, return num; Otherwise, the closest number in the range is returned.

def clamp_number(num,a,b):
 return max(min(num, max(a,b)),min(a,b))

give an example:

clamp_number(2, 3, 5) # 3
clamp_number(1, -1, -5) # -1

9. String: byte_size

Function implementation: return the number of bytes of the string.
Interpretation: use string Encode ('utf-8 ') decodes the given string and returns the length.

def byte_size(string):
 return len(string.encode('utf-8'))

give an example:

byte_size('😀') # 4
byte_size('Hello World') # 11

10. Math: gcd

Function realization: calculate the maximum common factor of several numbers.
Interpretation: use reduce() and math GCD is implemented on a given list.

from functools import reduce
import math

def gcd(numbers):
 return reduce(math.gcd, numbers)

give an example:

gcd([8,36,28]) # 4

The above is all kinds of tips for learning python in 30 seconds. In addition, there are many other skills that can be learned slowly. I hope it will be helpful to readers.

Python button skirt: 797403929

Topics: Python