current position:Home>Python code reading (Part 50): taking elements from list intervals
Python code reading (Part 50): taking elements from list intervals
2022-01-30 21:57:55 【FelixZ】
Python Code reading collection Introduction : Why not recommend Python Beginners directly look at the project source code
The code read in this article realizes every interval in a list n
The function of taking an element from a number .
The code snippet read in this article comes from 30-seconds-of-python.
every_nth
def every_nth(lst, nth):
return lst[nth - 1::nth]
# EXAMPLES
every_nth([1, 2, 3, 4, 5, 6], 2) # [ 2, 4, 6 ]
Copy code
every_nth
The function receives a list and the number of intervals nth
, Return the value result of the list .
The function uses list slicing , Get the original list every interval nth
The result of taking values of elements .
s[i:j:k]
The form of s
from i
To j
In steps of k
The section of .s
from i
To j
In steps of k
The slice of is defined as all that satisfy 0 <= n < (j-i)/k
The index number of x = i + n*k
A sequence of items . let me put it another way , The index number is i
,i+k
,i+2*k
,i+3*k
, And so on , When reach j
Stop when ( But it must not include j
). When k
When it is a positive value ,i
and j
Will be reduced to no more than len(s)
. When k
When it's negative ,i
and j
Will be reduced to no more than len(s) - 1
. If i
or j
Omitted or omitted None
, They will be defined as what can be reached at one end of the list “ End ” value ( The termination value at which end depends on k
The symbol of ). Please note that ,k
Cannot be zero . If k by
None, As if 1
Handle .
Because the index of the list is from 0
At the beginning , So the index of the first extracted number is nth-1
. because j
Omitted , So it will be calculated all the way to the end of the list . We use a less “ just ” Take an example of :
>>> every_nth([1, 2, 3, 4, 5, 6, 7], 2) # [ 2, 4, 6 ]
[2, 4, 6]
Copy code
copyright notice
author[FelixZ],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201302157532850.html
The sidebar is recommended
- Introduction to python (IV) dynamic web page analysis and capture
- leetcode 119. Pascal's Triangle II(python)
- leetcode 31. Next Permutation(python)
- [algorithm learning] 807 Maintain the city skyline (Java / C / C + + / Python / go / trust)
- The rich woman's best friend asked me to write her a Taobao double 11 rush purchase script in Python, which can only be arranged
- Glom module of Python data analysis module (1)
- Python crawler actual combat, requests module, python realizes the full set of skin to capture the glory of the king
- Summarize some common mistakes of novices in Python development
- Python libraries you may not know
- [Python crawler] detailed explanation of selenium from introduction to actual combat [2]
guess what you like
-
This is what you should do to quickly create a list in Python
-
On the 55th day of the journey, python opencv perspective transformation front knowledge contour coordinate points
-
Python OpenCV image area contour mark, which can be used to frame various small notes
-
How to set up an asgi Django application with Postgres, nginx and uvicorn on Ubuntu 20.04
-
Initial Python tuple
-
Introduction to Python urllib module
-
Advanced Python Basics: from functions to advanced magic methods
-
Python Foundation: data structure summary
-
Python Basics: from variables to exception handling
-
Python notes (22): time module and calendar module
Random recommended
- Python notes (20): built in high-order functions
- Python notes (17): closure
- Python notes (18): decorator
- Python notes (16): generators and iterators
- Python notes (XV): List derivation
- Python tells you what timing attacks are
- Python -- file and exception
- [Python from introduction to mastery] (IV) what are the built-in data types of Python? Figure out
- Python code to scan code to pay attention to official account login
- [algorithm learning] 1221 Split balanced string (Java / C / C + + / Python / go / trust)
- Python notes (22): errors and exceptions
- Python has been hidden for ten years, and once image recognition is heard all over the world
- Python notes (21): random number module
- Python notes (19): anonymous functions
- Use Python and OpenCV to calculate and draw two-dimensional histogram
- Python, Hough circle transformation in opencv
- A library for reading and writing markdown in Python: mdutils
- Datetime of Python time operation (Part I)
- The most useful decorator in the python standard library
- Python iterators and generators
- [Python from introduction to mastery] (V) Python's built-in data types - sequences and strings. They have no girlfriend, not a nanny, and can only be used as dry goods
- Does Python have a, = operator?
- Go through the string common sense in Python
- Fanwai 4 Handling of mouse events and solutions to common problems in Python opencv
- Summary of common functions for processing strings in Python
- When writing Python scripts, be sure to add this
- Python web crawler - Fundamentals (1)
- Pandas handles duplicate values
- Python notes (23): regular module
- Python crawlers are slow? Concurrent programming to understand it
- Parameter passing of Python function
- Stroke tuple in Python
- Talk about ordinary functions and higher-order functions in Python
- [Python data acquisition] page image crawling and saving
- [Python data collection] selenium automated test framework
- Talk about function passing and other supplements in Python
- Python programming simulation poker game
- leetcode 160. Intersection of Two Linked Lists (python)
- Python crawler actual combat, requests module, python to grab the beautiful wallpaper of a station
- Fanwai 5 Detailed description of slider in Python opencv and solutions to common problems