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

How to intercept python list list?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to intercept the python list list. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

And the value of the element of the list can be modified.

List format: (elements in the list can be string type, numeric type, Boolean type, etc.)

# Author:LJZlist= ['123 print print (x)] for i in range (4):

Execution result:

one hundred and twenty three

Abc

0

True

For the interception of the list (there are some details in this operation, which I summarize below)

Note: the forward index subscript of the list starts at zero, not 1, and the reverse index ends with-1, not 0

Intercepted format

Variable [header subscript: tail subscript]

Forward index (01234.)

1. Only the header subscript I and the colon (which represents the intercept from the element of the header subscript I to the end)

two。 Only the colon trailing subscript I (represents the element intercepted all the way to iMur1 from the beginning)

3. The header subscript I, the colon and the trailing subscript j all have (represents the element intercepted from I to jmur1)

The routine is as follows:

# Author:LJZlist= ['123 print print (y) print] x = list [1:] y=list [: 3] z=list [2:3] print (x) print (y) True

The results are as follows:

['abc', 0, True]

['123 dollars,' abc', 0]

[0]

Reverse index (.-2-1)

1. Only the header subscript I and the colon (which represents the intercept from the element of the header subscript I to the end)

two。 Only the colon trailing subscript I (represents the element intercepted all the way to iMur1 from the beginning)

3. The header subscript I, the colon and the trailing subscript j all have (represents the elements intercepted from I to jmur1

The routine is as follows:

# Author:LJZlist= ['123 print print (y) print (z)] x=list [- 3:] y=list [:-2] z=list [- 3 y=list 1] print (x)

The results are as follows:

['abc', 0, True]

['123 dollars,' abc']

['abc', 0]

Join the interception of step size:

The interception mentioned earlier has no step size. If you want to intercept the list with a certain number of elements, you can choose to use the interception method with step size.

Format:

Variable [header subscript: tail subscript: step size]

For example, if you intercept the third to fifth elements, if you intercept them two steps apart, you will intercept elements 3 and 5, and element 4 will be skipped.

Routine:

# Author:LJZlist= ['123 print print], "12345"] x = list [1: 4:2] True

Results:

['abc', True]

Add: python list intercept decomposition

#! / usr/bin/env python#coding:utf8 list1 = ['tzh', 786,2.23,' thz2', 70.2] list2 = [123 'tzh'] print list1 # output full list list1print list1 [0] # first element of list1 print list1 [1:3] # output list1 second to third elements print list1 [2:] # output list1 all elements from the third to the end of the list print list2 * 2 # output list List2 twice print list1 + list2 # print combination list list1 List2 about "how does the python list list intercept?" This is the end of this article. I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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