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

Analysis of list slicing examples of Python element set

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, the editor will share with you the relevant knowledge points of the list slice example analysis of the Python element collection, the content is detailed, and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

1. List slice (Slicing)

Since a list is a collection of elements, we should be able to get any subset of these elements. For example, if we want to get the first three elements from the list, we should be able to do it easily. The same is true for any three elements in the middle of the list, or the last three elements, or any x elements anywhere in the list. These subsets of the list are called slices.

If L is a list, the expression L [start: stop: step] returns the portion of the list from index start to index stop, at a step size step.

II. Basic examples

The following is a basic example of a list slice:

# Example: Slice from index 2 to 7L = ['axed,' baked, 'caged,' dashed, 'eyed,' faded, 'galled,' hacked,'i'] print (L [2:7]) # ['cased,' dumped, 'eyed,' faded,'g'] ['cased,' dumped, 'eyed,' f`' Third, slice with negative index (Slice with Negative Indices)

You can specify a negative index when you slice a list.

For example: Slice from index-7 to-2,

L = ['axiao,' baked, 'cased,' dashed, 'eyed,' faded, 'gashed,' hacked,'i'] print (L [- 7 _ () _ four, slices with positive or negative indexes.

You can specify both positive and negative indexes.

# Slice from index 2 to-5L = ['ahe,' baked, 'caged,' dashed, 'eyed,' faded, 'gaged,' hacked,'i'] print (L [2:-5]) # ['clocked,' d'] ['cased,' d'] 5. Specify the slice step

You can use the step parameter to specify the step size of the slice.

The step parameter is optional and is 1 by default.

# Returns every 2nd item between position 2 to 7L = ['axiao,' baked, 'caged,' dashed, 'eyed,' faded, 'galled,' hacked,'i'] print (L [2: 7:2]) # ['clocked,' eBay,'g'] ['clocked,' eBay,'g'] VI, negative step

You can specify a negative step.

# Example: Returns every 2nd item between position 6 to 1L = ['axiao,' baked, 'cased,' dashed, 'eyed,' faded, 'galled,' hacked,'i'] print (L [6: 1 print]) # ['gems,' eBay,'c'] ['gags,' eBay,'c'] 7. Slices at the beginning and end (Slice at Beginning & End)

Omitting the starting index slices from index 0.

Meaning, L [: stop] is equivalent to L [0:stop]

# Example: Slice the first three items from the listL = ['averse,' baked, 'cased,' dashed, 'estranged,' faded, 'gaged,' hacked,'i'] print (L [: 3]) # ['averse,' baked,'c'] ['averse,' baked,'c']

Omitting the stop index extends the slice to the end of the list.

It means that L [start:] is equivalent to L [start:len (L)]

Example: cut the last three items from the list

L = ['axed,' baked, 'cased,' dashed, 'eyed,' faded, 'gashed,' hacked,'i'] print (L [6:]) # ['Globe,' hinge,'i'] ['gathers,' hashes,'i'] VIII, inverted list (Reverse a List)

You can reverse the list by omitting to start the index and stop the index and specify the step as-1.

Example: reversing the list using the slice operator

L = ['averse,' baked, 'caged,' dumped,'e'] print (L [::-1]) ['eyed,' dashed, 'clocked,' baked,'a'] IX, modify multiple list element values

You can use slice assignments to modify multiple list elements at a time.

Example: use slice to modify multiple list items

L = ['asides,' baked, 'crested,' dudes,'e'] L [1:4] = [1, 2, 3] print (L) # ['asides, 1, 2, 3,' e'] ['asides, 1, 2, 3,' e']

Example: replace multiple components in place of a single component

L = [1:2] = [1, 2, 3] print (L) # ['asides, 1, 2, 3,' cations, 'dudes,' e'] ['asides, 1, 2, 3,' cations, 'dudes,' e'] 10, insert multiple list elements

We can insert items in the list without replacing anything. Just specify

Example: inserts multiple list items using slice

L = ['averse,' baked,'c'] L [: 0] = [1, 2, 3] print (L) # [1, 2, 3,'a', 'baked,' c'] [1, 2, 3,'a', 'baked,' c'] L = ['a', 'baked,' c'] L [len (L):] = [1, 2, 3] print (L) # [ 'baked,' crested, 1, 2, 3] ['axed,' baked, 'crested, 1, 2, 3]

You can insert an element in the middle of the list by specifying the start index and stop index of the slice.

Example: inserts multiple list items in the middle

L = ['averse,' baked,'c'] L [1:1] = [1, 2, 3] print (L) # ['asides, 1, 2, 3,' baked,'c'] ['asides, 1, 2, 3,' baked,'c'] 11, delete multiple list elements

You can delete multiple elements in the middle of the list by assigning the appropriate slice to the empty list.

You can also use del statements for slicing.

Example: delete multiple list items using slice

L = ['averse,' baked, 'clocked,' dumped,'e'] L [1:5] = [] print (L) # ['a'] ['a'] with del keywordL = ['axed,' baked, 'clocked,' dashed,'e'] del L [1:5] print (L) # ['a'] ['a'] XII, Clone or copy list

When new_List = old_List is executed, there are actually no two lists. The assignment copies only the reference to the list, not the actual list. Therefore, both new_List and old_List refer to the same list after assignment.

You can copy a list (also known as a shallow copy) using the slice operator.

Example: create a copy of the list using slice (shallow copy)

L1 = ['averse,' baked, 'caged,' dashed,'e'] L2 = L1 [:] print (L2) # ['axed,' baked, 'clocked,' dashed,'e'] print (L2 is L1) # False ['axed,' baked, 'cased,' d' The above'e'] False is all the content of the article "list slicing instance Analysis of Python element Collection" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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