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

What is the slice in Python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "what is slicing in Python". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Preface

What is slicing is actually part of the list. For example, the following list:

Players = ['james','anthony','wade','rose','curry']

You only want to choose these three names in the middle, you can do this:

Print (players [1:4]) ['anthony',' wade', 'rose']

Players [1:4] is a slice, preceded by variables corresponding to the list, followed by central brackets, starting index and ending index, concatenated by colons. The index also contains the left but not the right. For this example, it starts from 1 to 4, but does not include the element in the position of 4.

If you omit the starting index, it means that the index starts at 0:

Print (players [: 4]) ['james',' anthony', 'wade',' rose']

If you omit the terminating index, it means that the slice fetches the last element of the list:

Print (players [1:]) ['anthony',' wade', 'rose',' curry']

Negative numbers can also be used in the index, meaning that elements are truncated from the end of the list:

Print (players [- 2:]) ['rose',' curry'] ergodic slices

For loops can also be used in slices:

For player in players [: 3]: print (player.title ())

The results are as follows:

JamesAnthonyWade copies the entire list

If you want to copy the entire list, you actually take the slice from beginning to end, then you can omit both the starting index and the ending index, leaving only colons:

New_players = players [:]

To verify that the entire list is indeed copied, we add a different element to each of the original list players and the newly copied list new_players, and then print them separately:

Players.append ('thompson') new_players.append (' durant') print (players) print (new_players)

Let's take a look at the results, and it is true that the list has been completely copied:

['james',' anthony', 'wade',' rose', 'curry',' thompson'] ['james',' anthony', 'wade',' rose', 'curry',' durant']

Note that the colon when copying the list must not be forgotten. Let's see what happens if you forget the colon:

Players = ['james','anthony','wade','rose','curry'] new_players = playersplayers.append (' thompson') new_players.append ('durant') print (players) print (new_players)

Focus on the second line of code. Instead of using slicing, we directly associate the new list new_players to the old list players. Next, we still add different elements to the two lists. Let's take a look at the printed results:

['james',' anthony', 'wade',' rose', 'curry',' thompson', 'durant'] [' james', 'anthony',' wade', 'rose',' curry', 'thompson',' durant']

We found that the two lists are exactly the same, and the two newly added elements have been added to it. Why?

In fact, as we said before, a variable can be thought of as a label that points to something.

In this example, the variable players points to the list ['james','anthony','wade','rose','curry']. In line 2, the new variable new_players is associated with the variable players, so the new variable new_players also points to the list ['james','anthony','wade','rose','curry'].

Therefore, no matter which variable is added later, they all point to the same list, so the two lists output will be exactly the same.

This is the end of the content of "what is the slice in Python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report