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 basic operation of Python sequence type

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "what is the basic operation of Python sequence types". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the basic operation of Python sequence types"?

1 Overview

Before introducing the common data types of Python, let's take a look at the most basic data structure of Python-sequence. One of the characteristics of a sequence is that it fetches the elements in the sequence according to the index (index, that is, the position of the element). The first index is 0, the second index is 1, and so on.

All sequence types can perform some common operations, such as:

Index (indexing)

Fragmentation (sliceing)

Iteration (iteration)

Add (adding)

Multiply (multiplying)

In addition to the above, we can also check whether an element is a member of a sequence, calculate the length of the sequence, and so on.

Focus on indexing and slicing.

2 basic Operations 2.1 Index

The elements in the sequence can be obtained through the index, which starts at 0. Look at the following example:

> nums = [1,2,3,4,5] # list > nums [0] 1 > nums [1] 2 > nums [- 1] # Index-1 indicates the last element 5 > s = 'abcdef' # string > s [0]' a' > s [1]'b' > a = (1, 2, 3) # tuple > a [0] 1 > a [1] 2

Notice that-1 represents the last element of the sequence,-2 represents the penultimate element, and so on.

2.2 slicing

The index is used to get a single element in the sequence, while fragments are used to get some elements of the sequence. The sharding operation needs to provide two indexes as boundaries, separated by colons, such as:

> numbers = [1,2,3,4,5,6] > > numbers [0:2] # list fragmentation [1,2] > > numbers [2:5] [3,4,5] > s = 'hello, world' # string sharding > s [0:5]' hello' > > a = (2,4,6,8,10) # tuple shard > a [2:4] (6,8)

It should be noted here that the shard has two indexes, and the element of the first index is included, while the index of the second element is not included, that is, numbers [2:5] gets numbers [2], numbers [3], numbers [4], not including numbers [5].

Here are some techniques for using slicing:

(1) access the last few elements

(2) use step size

Access the last few elements:

Assuming that we need to access the last three elements of the sequence, we can certainly do this as follows:

> numbers = [1,2,3,4,5,6] > > numbers [3:6] [4,5,6]

Is there a simpler way? Thinking that you can use indexes in the form of negative numbers, you might try to do this:

> numbers = [1, 2, 3, 4, 5, 6] > > numbers [- 3 numbers] # what is actually fetched is numbers [- 3], numbers [- 2] [4, 5] > numbers [- 3:0] # elements in the left index appear later than those in the right index, and an empty sequence is returned []

The above two uses do not correctly get the last three elements of the sequence, and Python provides a shortcut:

(that is, if you want the shard to contain the last element, you can leave the second index blank)

> numbers = [1,2,3,4,5,6,7,8] > > numbers [- 3:] [6,7,8] > > numbers [5:] [6,7,8]

If you want to copy the entire sequence, you can leave both indexes empty:

> numbers = [1, 2, 3, 4, 5, 6, 7, 8] > > nums = numbers [:] > > nums [1, 2, 3, 4, 5, 6, 7, 8]

Use step size:

When using sharding, the step size is 1 by default, that is, access one by one, or you can customize the step size, such as:

> > numbers = [1, 2, 3, 4, 5, 6, 7, 8] > numbers [0:4] [1, 2, 3, 4] > > numbers [0:4:1] # step size is 1, or you don't have to write it. Default is 1 [1, 2, 3, 4] > > numbers [0:4:2] # step size is 2, take out numbers [0], numbers [2] [1, 3] > numbers [: 3] # is equivalent to numbers [0:8:3] Fetch elements with index 0,3,6 [1,4,7]

In addition, the step size can also be negative, indicating that the element is taken from right to left:

> > numbers = [1, 2, 3, 4, 5, 6, 7, 8] > > numbers [0: 4 numbers 1] [] > numbers [4: 0 numbers 1] # take out elements with index 4, 3, 2, 1 [5, 4, 3, 2] > > numbers [4: 0 2] # take out elements with index 4, 2 [5, 3] > numbers [::-1] # remove all elements from right to left [8,7,6,5] 4, 3, 2, 1] > > numbers [::-2] # take out the elements with index 7, 5, 3, 1 [8, 6, 4, 2] > > numbers [6::-2] # take out the elements with index 6, 4, 2, 0 [7, 5, 3, 1] > numbers [: 6: 6-3] # take out the elements with index 7 [8]

Here is a summary of some methods of sharding operation. The forms of sharding operation are:

# left index: right index: step left_index:right_index:step

One thing to keep in mind is:

The elements of the index on the left are included in the results, while those of the index on the right are not included in the results.

When using a negative number as the step size, you must make the left index larger than the right index.

Take elements from left to right for positive steps and from right to left for negative steps

2.3 plus

Sequences can be "added" as follows:

> [1,2,3] + [4,5,6] # "addition" effect is actually linked together [1,2,3,4,5,6] > (1,2,3) + (4,5,6) (1,2,3,4,5,6) > 'hello,' + 'worldview' > [1,2,3] + 'abc' Traceback (most recent call last): File ", line 1 In TypeError: can only concatenate list (not "str") to list# what you need to note here is that two sequences of the same type can be "added". 2.4 times

Sequences can be multiplied, such as:

> 'abc' * 3' abcabcabc' > [0] * 3 [0,0,0] > > [1, 2, 3] * 3 [1, 2, 3, 1, 3, 3] so far, I believe you have a deeper understanding of "what is the basic operation of Python sequence types". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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