In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what are the tips for writing Python?". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the tips for writing Python?"
1. List:all_equal
Functional implementation: verify that 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]
For example:
All_equal ([1, 2, 3, 4, 5, 6]) # False all_equal ([1, 1, 1]) # True
2. List:all_unique
Function implementation: if all values in the list are unique, return True, otherwise False
Interpretation: use the collection set () to remove duplicates on a given list and compare it with the length of the original list.
Def all_unique (lst): return len (lst) = = len (set (lst))
For example:
X = [1, True all_unique, 3, 4, 5, 5, 6] y = [1, True all_unique, 2, 3, 4, 5] False
3. List:bifurcate
Function implementation: group list values. If the element in filter is True, then the corresponding element belongs to the first group; otherwise, it belongs to the second group.
Interpretation: use list derivation and enumerate () to each group based on filter elements.
Def bifurcate (lst, filter): return [[x for iMagne x in enumerate (lst) if filter [I] = = True], [x for iMagne x in enumerate (lst) if filter [I] = = False]]
For example:
Bifurcate (['beep',' boop', 'foo',' bar'], [True, True, False, True]) # [['beep',' boop', 'bar'], [' foo']]
4. List:difference
Functional implementation: returns the difference between the two iterables.
Interpretation: create a collection of b and use the list derivation of a to retain elements that are not in _ b.
Def difference (a, b): _ b = set (b) return [item for item in an if item not in _ b]
For example:
Difference ([1,2,3], [1,2,4]) # [3]
5. List:flatten
Function realization: an one-time integrated list.
Interpretation: use nested lists to extract each value of a sublist.
Def flatten (lst): return [x for y in lst for x in y]
For example:
Flatten ([[1, 2, 3, 4], [5, 6, 7, 8])) # [1, 2, 3, 4, 5, 6, 7, 8]
6. Math:digitize
Function realization: decompose a number into individual digits.
Interpretation: after characterizing n, use the map () function combined with int to complete the conversion
Def digitize (n): return list (map (int, str (n)
For example:
Digitize (123) # [1,2,3]
7. List:shuffle
Function realization: randomly disrupt the order of list elements.
Interpretation: use the Fisher-Yates algorithm to reorder list elements.
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
For example:
Foo = [1JI 2jue 3] shuffle (foo) # [2JI 3JI 1], foo = [1Jet 2Jue 3]
8. Math:clamp_number
Function realization: put the digital num clamp in the range specified by the boundary values of an and b.
Interpretation: if num falls within the range, return num; otherwise, return the nearest number in the range.
Def clamp_number (num,a,b): return max (min (num, max (aMaginb)), min (aMaginb))
For example:
Clamp_number (2,3,5) # 3 clamp_number (1,-1,-5) #-1
9. String:byte_size
Function implementation: returns the number of bytes of the string.
Interpretation: use string.encode ('utf-8') to decode a given string and return the length.
Def byte_size (string): return len (string.encode ('utf-8'))
For example:
Byte_size ('?') # 4 byte_size ('Hello World') # 11
10. Math:gcd
Function realization: calculate the maximum common factor of several numbers.
Interpretation: implemented on a given list using reduce () and math.gcd.
From functools import reduce import math def gcd (numbers): return reduce (math.gcd, numbers)
For example:
Gcd ([8: 36 / 28]) # 4 so far, I believe you all have a deeper understanding of "what are the tips for writing Python". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.