Is there such a problem that you really understand python programming from now on?

Tianyuan prodigal son 2020-11-13 09:09:30
problem really understand python programming


When I write down this topic , In my mind, I couldn't help but hear Zhou Huajian's slightly hoarse voice :

The familiar song came from afar ,
Why are those voices so weak .
long time no see , Are you all right now ?
Is there such a song ,
Will let you follow and ,
With the ups and downs of our lives ,
A theme song to sing together ;
Is there such a song ,
It will remind you of me ,
Make you happy and worry ,
Such a me ……

The music is over , Back to the point . Browse recently LeetCode, I found an interesting little topic . When I try Python When you answer , Actually used the assembly 、map function 、zip function 、lambda function 、sorted function , The debugging process also involves iterators 、 generator 、 The concept of list derivation . A seemingly simple topic , Although the final code can be combined into one line , But almost put Python I used the programming skills of , It can be said. “ The subtlety is the spirit ”! Through this topic , Maybe it will make you really understand Python Programming .

This question , be known as 《 The lucky number in the list 》. What is the lucky number ? In the list of integers , If a number appears at the same frequency as its value , We call this number 「 Lucky number 」. for example , In the list [1, 2, 2, 3] in , Numbers 1 And number 2 The number of times they appear is 1 and 2, So they are lucky numbers , but 3 Only ever 1 Time ,3 It's not a lucky number .

I understand the concept of lucky number , Let's try to find out the list [3, 5, 2, 7, 3, 1, 2 ,4, 8, 9, 3] The lucky number in . This process can be divided into the following steps :

  1. Find out the numbers that are not repeated in the list
  2. Count the number of times each number appears in the list
  3. Find out the numbers that appear equal to the number itself

The first 1 Step , Find out the numbers that are not repeated in the list

Find out the numbers that are not repeated in the list , That is, to remove duplicate elements from the list , abbreviation “ duplicate removal ”. The simplest way to de duplicate is to use sets .

>>> arr = [3,5,2,7,3,8,1,2,4,8,9,3]
>>> unique = set(arr)
>>> unique
{
1, 2, 3, 4, 5, 7, 8, 9}

The first 2 Step , Count the number of times each number appears in the list

We know , The list object comes with a count() Method , Can return the number of times an element appears in the list , The usage is as follows :

>>> arr = [3,5,2,7,3,8,1,2,4,8,9,3]
>>> arr.count(8) # Elements 8 In the array arr There has been 2 Time 
2

Next , We only need to traverse the elements after de duplication , Count the number of times they appear one by one , And save it into a suitable data structure , This step of the work will be all right .

>>> arr = [3,5,2,7,3,8,1,2,4,8,9,3]
>>> unique = set(arr) # Remove duplicate elements 
>>> pairs = list() # An empty list , Used to hold tuples of array elements and occurrence times 
>>> for i in unique:
pairs.append((i, arr.count(i)))
>>> pairs
[(1, 1), (2, 2), (3, 3), (4, 1), (5, 1), (7, 1), (8, 2), (9, 1)]

As a rookie , Code like this , Has been very good . however , A aspiring programmer will never be complacent about it 、 come to a standstill . Their favorite thing to do is to do everything possible to eliminate for loop , For example, using mapping functions 、 Filter function instead of for loop ; Even if you can't refuse for loop , They also try to hide the cycle as much as possible , For example, hiding in list derivation . Since this is to call the list for every element count() This method , That's the best way to use map Function instead of for Circulated .

>>> m = map(arr.count, unique)
>>> m
<map object at 0x0000020A2D090E08>
>>> list(m) # Generators can be converted to lists 
[1, 2, 3, 1, 1, 1, 2, 1]
>>> list(m) # The generator can only be used once , After use , It's automatically cleaned up 
[]

map The function returns a generator (generator), It can be traversed like a list , But you can't see the elements as intuitively as a list , Unless we use list() Turn this generator into a list ( You don't actually need to turn the generator into a list ). Please note that , Generators are different from iterators , Or a generator is a special kind of iterator , Can only be traversed once , End of traversal , It's gone . Iterators can iterate over and over . such as ,range() Function returns iterator :

>>> a = range(5)
>>> list(a)
[0, 1, 2, 3, 4]
>>> list(a)
[0, 1, 2, 3, 4]

Say generator and iterator , We have to go back to the original topic . Use map Mapping function , We get the number of occurrences of each element , You also need to form a tuple with the corresponding elements . Now , Just use zip() Function .zip() Function to create a generator , Used to aggregate every iteratable object ( iterator 、 generator 、 list 、 Tuples 、 aggregate 、 String, etc. ) The elements of , Elements aggregate according to the same subscript , If the length is different, elements larger than the shortest iteration object length will be ignored .

>>> m = map(arr.count, unique)
>>> z = zip(unique, m)
>>> z
<zip object at 0x0000020A2D490508>
>>> list(z)
[(1, 1), (2, 2), (3, 3), (4, 1), (5, 1), (7, 1), (8, 2), (9, 1)]
>>> list(z)
[]

Obviously ,zip() The function also returns the generator , It can only be used once , Then it disappears .

The first 3 Step , Find out the numbers that appear equal to the number itself

With each element and its number of occurrences , We just need to loop through …… No , One moment please , Why do we have to cycle ? We just need to filter every element , Find out the tuples that have the same number of occurrences as the element itself , Why not try the filter function filter() Well ?

>>> def func(x): # Parameters x It's a tuple type 
if x[0] == x[1]:
return x
>>> m = map(arr.count, unique)
>>> z = zip(unique, m)
>>> f = filter(func, z)
>>> f
<filter object at 0x0000020A2D1DD908>
>>> list(f)
[(1, 1), (2, 2), (3, 3)]
>>> list(f)
[]

Filter function filter() Take two parameters , The first 1 A parameter is a function , Used to determine whether an element meets the filtering conditions , The first 2 One parameter is the iteratable object to be filtered .filter() The function also returns the generator , It can only be used once , Then it disappears .

Write here , We're almost done . however , As a pursuing programmer , You can tolerate func() Such a strange looking function ? The answer is no ! You must be able to use lambda Function to replace it . in addition , Maybe we need to sort the results by the size of the elements . Add sorting , The complete code is as follows :

>>> arr = [3,5,2,7,3,8,1,2,4,8,9,3]
>>> unique = set(arr)
>>> m = map(arr.count, unique)
>>> z = zip(unique, m)
>>> f = filter(lambda x:x[0]==x[1], z)
>>> s = sorted(f, key=lambda x:x[0])
>>> print(' The lucky number is :', [item[0] for item in s])
The lucky number is : [1, 2, 3]

The ultimate code , One line fix

If you've ever been written in one line by those 、 But it can achieve complex functions 、 The painful experience of code ravaging that looks like a heavenly book , that , Now you can also write the above code in one line , To ravage others .

>>> arr = [3,5,2,7,3,8,1,2,4,8,9,3]
>>> print(' The lucky number is :', [item[0] for item in sorted(filter(lambda x:x[0]==x[1], zip(set(arr), map(arr.count, set(arr)))), key=lambda x:x[0])])
The lucky number is : [1, 2, 3]

Dramatic reversal , I really understand this time Python 了 !

This blog has not been published for two days , Some netizens left a message saying , Why bother ? It's not easier to write like this 、 Is it easier to read ? Sure enough , I really want to !

>>> arr = [3,5,2,7,3,8,1,2,4,8,9,3]
>>> [x for x in set(arr) if x == arr.count(x)]
[1, 2, 3]
版权声明
本文为[Tianyuan prodigal son]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database