Get the App
SLTechnology News&Howtos  ›  Development  › 

How python handles some elements of a list

Shulou Source: shulou.com Published: 2022-06-03 09:47:47 09月29日 Update

This article mainly introduces python how to deal with some elements of the list, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

1. Some elements of the processing list are called slices. Create a slice and specify the index of the first element and the last element to be used.

2. This allows Python to create a slice that starts with the first element and ends with the last element, that is, copying the entire list.

Example

Names = ['zhang_san','chen_cheng','li_hong','liu_li','chen_yu'] print (names [0:3]) print (names[ 0:-1]) print (names [:]) print (names [- 1]) print (names [- 3:]) negative index returns the element corresponding to the distance from the end of the list. To output the last three members of the list, use the slice names [- 3:] as the function range () Python stops ['zhang_san',' chen_cheng', 'li_hong'] [' zhang_san', 'chen_cheng',' li_hong', 'liu_li'] [' zhang_san', 'chen_cheng',' li_hong', 'liu_li',' chen_yu'] chen_yu ['li_hong',' liu_li'] after reaching the element in front of the second index you specify. [chen_yu'] Thank you for reading this article carefully. I hope the article "how to deal with some elements of the list" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Elements articles sections indexes values interests functions at the same time lists examples more friends end knowledge editing industry negative numbers information channels team members Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei vpn MariaDB macOS Shulou Technology