Home >Backend Development >Python Tutorial >python sequence list

python sequence list

巴扎黑
巴扎黑Original
2017-07-20 09:34:471285browse

Note: The test environment of this article is python 2.7

List, unlike an array, can store the same type of data or different types of data

list1 = [1,2,3]

list2 = [1,'hello',False]

The list belongs to the sequence, so it takes into account some common characteristics of the sequence

1. Index

list1 = [1,2,3,4]

The index of the list is from left to right, starting from 0, followed by +1

s1[0] represents the first element, where s1[0] = 1

Take the index from right to left, starting from -1, and then -1

s1[-1] Represents the last element, here s1[-1] = 4

2. Fragmentation

The fragmentation operator is ':' (colon), fragmentation operation It is a very convenient way to generate a new list

s1 = [1,2,3,4]

s2 = s1[1:3] #The first parameter is the beginning Index, the second parameter is the end index (the element at the end index cannot be obtained, 1:3 can only obtain indexes 1 and 2)

Output result: s2 = [2,3]

s3 = s1[1:-1] #Here index -1 is equivalent to the above 3

Output result: s3 = [2,3]

Note 1: Both parameter 1 and parameter 2 can omit parameter 1. When omitted, it represents the starting index 0 and parameter 2. When omitted, it represents the maximum position index

Note 2: s1[-1,1] = [ ] , get the starting index from the last index, so the return is an empty list. In fact, when the index on the left appears later than the index on the right, an empty list will be returned

When we need to fetch the entire list, we introduce the concept of step size

s1[a:b :c], the sharding operation actually has three parameters. Parameters 1 and 2 have already been introduced. Parameter 3 is the step size. The default step size can be omitted. The default is 1

, so s1[:] returns the entire The list additionally s1[::-1] returns the entire reversed list

Regarding the sharding operation, we write the following lines of code

s1 = [1,2, 3,4]

s2 = s1 #Assign s1 to s2

s3 = s1[:] #Assign the list of s1 to s3

print s1==s2 #Judge whether the values ​​of s1 and s2 are the same

print s1==s3

print s1 is s2 #Judge whether s1 and s2 are the same sequence

print s1 is s3

In order to copy a new sequence, direct assignment is obviously not possible. We use sharding

3, addition/multiplication

s1 = [1,2,3]

s2 = [4,5,6]

s = s1+s2 After adding the two lists, return the new list s=[1,2 ,3,4,5,6]

s3 = 'hello'

s4 = (1,2,3)

s1+s3

s1+s4

After this statement is executed, an error will be reported

Note: Although strings and tuples are sequences like lists, they cannot be directly related. Add

Here: Introduce a built-in function list(), which can directly convert a string or tuple into a string s1 + list(s3) s1 + list(s4) In this way, it can be executed normally

Note: list() can convert sequences such as strings and tuples into lists. How to convert lists into strings?

eg:s = ''.join(list1), you can convert the list list1 into a string s

Multiplication of the list s1 = [1]

s2 = s1*10

Return s2 = [1,1,1,1,1,1,1,1,1,1]

Commonly used to declare an empty list

s1 = [None] #It is not possible to use s1 = [] at this time

s2 = s1*10

Declares an empty list with a length of 10

4. Check whether the element exists s1 = [1,2,3]

if 1 in s1:

print "Element exists"

else:

print "Element does not exist"

5. Commonly used built-in functions for lists

list1 = [1,2,3, 4]

len(list1) #Get the length of the list

max(list1) #Get the maximum value of the list

min(list1) #Get the list Minimum value

sum(list1) #Sum the list members. The summed list members cannot contain strings, otherwise an error will be reported.

cmp(list1,list2) #Compare two lists The size of Compare

all are consistent, compare length, if the longer one is the same, it will be regarded as equal, return 0

The above is the detailed content of python sequence list. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn