In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-11-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you the Python slice iterative list generation and generator example analysis, I hope you will learn something after reading this article, let's discuss it together!
In Python, the less code the better, and the simpler the better. Based on this idea, you need to master the very useful advanced features of Python, one line of code can achieve the function, never write five lines of code. The less code, the more efficient the development.
Slice
Tuple,list, string can be sliced
L = ['Michael',' Sarah', 'Tracy',' Bob', 'Jack'] L [0:3] # [' Michael', 'Sarah',' Tracy'] L [: 3] # ['Michael',' Sarah', 'Tracy'] L [1:3] # [' Sarah', 'Tracy'] L [- 2:] # [' Bob' 'Jack'] L [- 2 list] L [: 10] # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] L [- 10:] # [90, 91, 92, 93, 94, 95, 96, 97, 98, 99] L [10:20] # [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] L [: 10:2] 2, 4, 6, 8] L [:: 5] # [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95] L [:] # [0, 1, 2, 3,..., 99]
Practice
Using the slicing operation, implement a trim () function to remove the spaces at the beginning and end of the string, and be careful not to call the strip () method of str:
#-*-coding: utf-8-*-def trim (s): for i in range (0Len (s)): if s [0] =': s = s [1:] elif s [- 1] = ='': s = s [:-1] return s iteration
Any iterable object can act on the for loop, including our custom data type. As long as the iteration condition is met, the for loop can be used.
How do you tell if an object is iterable? The method is to determine the Iterable type of the collections.abc module.
Python's built-in enumerate function turns a list into an index-element pair that iterates over both the index and the element itself in the for loop.
For I, value in enumerate (['Aids,' bones,'C']): print (I, value)
Practice
Use iterations to find the minimum and maximum values in a list and return a tuple:
#-*-coding: utf-8-*-def findMinAndMax (L): max = min = None if (len (L) > 0): l = list (L) max = min = L [0] for i in L: if I > max: max = i if i
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.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.