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 is the technique of meeting too late in Python

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

Share

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

This article shows you what the technique of meeting each other too late in Python refers to. The content is concise and easy to understand, and it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Do not say much, just dry, saved for a long time!

1. Swap variable value

This should be relatively simple, but it is easy to ignore in daily use.

A, b = 5,10 print (a, b) / / 5,10a, b = b, a print (a, b) / / 10,52. Combine all elements in the list into a string

This is actually a basic grammar.

A = ['python',' java', 'caterpillar python,java,c++,go3,' go'] print (', '.join (a)) / / python,java,c++,go3. Find the most frequent value in the list

Doesn't it feel good to use Python to do the math?

A = [1, 1, 1, 2, 3, 3, 3, 3, 3, 4, 4] print (max (set (a), key = a.count)) 4. Check whether the two strings are made up of the same alphabetical disorder from collections import Counter a = 'abcdefg' b =' adcbgfb' print (Counter (a) = = Counter (b)) 5. Reverse strings, lists

This can be achieved in one sentence with Java.

A = 'dadabjdnakdmnkafad' print (a [::-1]) num = 1343453535 print (int (str (num) [::-1])) a = [1 print (a [::-1]) 6. Transpose the two-dimensional array origin = [['axiang,' b'], ['centering,' d'], ['eBay,' f']] transposed = zip (* origin) print (list (transposed)) 7. Chain comparison

This is more in line with the mathematical habits.

B = 6 print (4 < b < 7) print (1 = = b < 9) 8. Ternary operator

In fact, there is no ternary operator in Python, but we can replace it in another way:

B = 'B'c =' C' flag = True a = b if flag else c9. Chain function call def product (a, b): return a * b def add (a, b): return a + b = True print ((product if b else add) (5,7)) 10. For-else syntax

Note that instead of if-else, the for loop can use else:

A = [1,2,3,4,5] for el in a: if (el = = 0) print ('found 0') else: print ('not found 0') 11. The merge dictionary D1 = {'averse: 1} D2 = {' baked: 2} print (* * D1, * * D2) # python3.5 supports print (dict (d1.items () | d2.items ()) d1.update (D2) print (D1) 12. Remove the repeating element items = [2 list (set (items) 13. * * kwargs from the list

Variable length parameter, which is a dictionary.

The double asterisk in front of the dictionary object allows you to enter the contents of the dictionary into the function as a named parameter. The secret key of the dictionary is the parameter name, and the value is the value passed to the function. You don't even have to call it kwargs!

Dictionary = {'key:', key: 1,' baked: 2} def func (* * kwargs) for key in kwargs: print ('key:', key,' value:',kwargs [key]) 14. List derivation

You can complete the data manipulation in the list with one line of code

Numbers = [1, 2, 3, 4, 5, 6] y = [x for x in numbers if x% 2 = = 0] print (y) / / [2, 4, 6] 15. Map function x = [1, 2, 3] y = map (lambda x: X + 1, x) print (y) / / [2, 3, 4] what are the skills of meeting too late in Python? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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