Python basic knowledge points: slice

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only, and do not have any commercial use. If you have any questions, please contact us for processing.

The following article comes from the decision not to pat the head, the author Dark Horse

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

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

You only want to take the middle three names, you can do this:

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

players[1:4] is a slice, the front is the variable corresponding to the list, the back is enclosed in square brackets, and the inside is the start index and the end index, and the middle is connected by a colon. This index also includes the left and not the right. For this example, it starts from 1 and ends at 4, but does not include the element at position 4.

If the starting index is omitted, it means that the index starts at 0:

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

If the end index is omitted, it means that the slice will get to the last element of the list:

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

Negative numbers can also be used in the index, which means that elements are intercepted from the end of the list:

print(players[-2:])
['rose', 'curry']

 

Iterate over slices

A for loop can also be used in slicing:

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

The results are as follows:

James
Anthony
Wade

 

Copy the entire list

If you want to copy the entire list, you are actually taking the slice from the beginning to the end, so you can omit the start index and the end index at the same time, leaving only the colon:

new_players = players[:]

In order to verify that the entire list is indeed copied, we add a different element to 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 indeed the list has been copied in its entirety:

['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 we forget the colon:

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

Focus on the second line of code. Instead of using slices, we directly associate the new list new_players with 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 them. Why?

In fact, as we said before, a variable can be regarded as a label, which points to a certain content.

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

Therefore, no matter which variable you add new elements to, they all point to the same list, so the two lists will be exactly the same in the final output.

Guess you like

Origin blog.csdn.net/m0_48405781/article/details/114835170