Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the super practical Python?

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what super-practical Python". In daily operation, I believe many people have doubts about which super-practical Python problems. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "what super-practical Python there are?" Next, please follow the editor to study!

Catalogue

1. Check for duplicate elements

two。 Transpositional words

3. Check memory usage

4. Byte size calculation

5. Print the string repeatedly N times

6. Initials are capitalized

7. Divide into blocks

8. Compress

9. Interval number

10. Chain comparison

In this article, we will introduce 30 short code snippets that you can understand and learn in 30 seconds or less.

1. Check for duplicate elements

The following method checks whether there are duplicate elements in a given list. It uses the set () attribute, which removes duplicate elements from the list.

Def all_unique (lst): return len (lst) = = len (set (lst)) x = [1, False all_unique, 2, 2, 3, 3, 3, 4, 5, 5] y = [1, 2, 3, 5] all_unique (x) # False all_unique (y) # True2. Transpositional words

Detect whether two strings are positional words for each other (that is, reverse the order of characters)

From collections import Counter def anagram (first, second): return Counter (first) = Counter (second) anagram ("abcd3", "3acdb") # True3. Check memory usage

The following code snippet can be used to check the memory usage of an object.

Import sys variable = 30 print (sys.getsizeof (variable)) # 244. Byte size calculation

The following method returns the string length in bytes.

Def byte_size (string): return (len (string.encode ('utf-8') byte_size (') # 4 byte_size ('Hello World') # 115. Print the string repeatedly N times

The following code prints a string n times without using a loop

N = 2; s = "Programming"; print (s * n); # ProgrammingProgramming6. Initials are capitalized

The following code snippet capitalizes each word in the string using the title () method.

S = "programming is awesome" print (s.title ()) # Programming Is Awesome7. Divide into blocks

The following method uses range () to block the list into smaller lists of the specified size.

From math import ceil def chunk (lst, size): return list (map (lambda x: lst [x * size:x * size + size], list (range (0, ceil (len (lst) / size) chunk) Compress

The following method uses fliter () to delete error values from the list (such as: False, None, 0 and ")

Def compact (lst): return list (filter (bool, lst)) compact ([0,1, False, 2,', 3,'a cow,'a horse, 34]) # [1, 2, 3,'a cow, 34] 9. Interval number

The following code snippet can be used to convert a 2D array.

Array = [['averse,' b'], ['croup,' d'], ['eBay,' f']] transposed = zip (* array) print (transposed) # [('axiang,' censor,'e'), ('baked,' dumped,'f')] 10. Chain comparison

The following code can be compared multiple times in one line with various operators.

A = 3 print (2 < a < 8) # True print (1 = = a < 2) # False, the study on "what are the super practical Python" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report