Use part of the list: python slice

Learn how to deal with all the elements of the list. Part of the element processing list --Python called slices.

slice

To create a slice, you can specify the index of the first element to be used and the last element.

A function range () as, the Python is stopped after a specified preceding the second index reaches an element.

To output the first three elements of the list, specify the index 0-3 , which outputs are 0 , 1 and 2 elements.

The following example deals with a sports team member list:

players = ['charles', 'martina', 'michael', 'florence', 'eli']

print(players[0:3])

Print a slice of the list, which contains only three players. Output is a list that contains the top three players:

['charles', 'martina', 'michael']

Any subset of the list may be generated, for example, if the list to be extracted of 2 to 4 elements, may be designated as starting index 1 and the index designated as the terminating 4 :

players = ['charles', 'martina', 'michael', 'florence', 'eli']

print(players[1:4])

This time, the slice begins with 'Marita' , and finally 'florence' :

>>>

['martina', 'michael', 'florence']

If you do not specify the first index, Python will automatically start from the beginning of the list:

players = ['charles', 'martina', 'michael', 'florence', 'eli']

print(players[:4])

Without specifying the starting index, Python start extracting from the beginning of the list: ,

>>>

['charles', 'martina', 'michael', 'florence']

Let the slice terminated at the end of the list, also can use a similar syntax.

For example, to extract from the 3 all elements to the end of the list of elements, may be designated as starting index 2 and will not be terminated Index:

players = ['charles', 'martina', 'michael', 'florence', 'eli']

print(players[2:])

Python returns from 3 all the elements of elements to the end of the list:

>>>

['michael', 'florence', 'eli']

No matter how long the list, this syntax can export all elements from a particular location to the end of the list.

Negative indices returns the element corresponding distance from the end of the list, so that any slice can be output end of the list.

For example, if you want to output the final three players on the list, you can use sliced the Players [-3:] :

players = ['charles', 'martina', 'michael', 'florence', 'eli']

print(players[-3:])

The code prints the name of the last three players, even if the length of the team roster changes, remains the same.

>>>

['michael', 'florence', 'eli']

Traverse sections

If part of the element to traverse the list, it may be for use slice loop. In the following example, we traverse the top three players, and print their names:

players = ['charles', 'martina', 'michael', 'florence', 'eli']

print("Here are the first three players on my team:")

for player in players[:3]:

    print(player.title())

Code does not traverse the entire list of players, but only the top three players to traverse:

>>>

Here are the first three players on my team:

Charles

Martina

Michael

In many cases, sections are useful.

When writing the game, the player can exit the game to its final score added to a list.

To acquire three of the player's highest score, you can arrange the list in descending order, and then create a score that contains only the first three sections.

When processing data, the slice may be used to batch processing;

编写Web应用程序时,可使用切片来分页显示信息,并在每页显示数量合适的信息。

players = ['charles', 'martina', 'michael', 'florence', 'eli']

print("Here are the first three players on my team:")

for player in players[:3]:

    print(player.title())

print("Here are the players on my team:")

for player in players:

    print(player.title())

print("Here are the players on my team:")

players=[player.title() for player in players]

for player in players:

    print(player)

print(players[-3:])

>>>

Here are the first three players on my team:

Charles

Martina

Michael

Here are the players on my team:

Charles

Martina

Michael

Florence

Eli

Here are the players on my team:

Charles

Martina

Michael

Florence

Eli

['Michael', 'Florence', 'Eli']

>>>

 

Guess you like

Origin www.cnblogs.com/shwj/p/12642742.html