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

How to use the list of getting started with Python

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

Share

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

This article will explain in detail how to use the list of beginners of Python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

What is a list

In order to store the operational data conveniently, python language provides some built-in data structures.

For example: lists, tuples, dictionaries, collections, etc. The so-called "built-in" means that they are part of the python language

So you can use them directly in the program.

1. The structure of the list

A list of python, equivalent to an array of other programming languages.

>

> mylist

[1, 2, 3, 4]

2. The characteristics of the list

All the data in the ● list is arranged in order, that is, the list belongs to the sequential type.

All the data found in the ● list is an index of two integer types, which can always be mapped to uniquely determined data by setting an index.

Duplicate data can exist in the ● list

Any type of data can be stored in the ● list, and multiple types of data can be stored mixed.

The ● list can be dynamically scaled and scaled as needed, that is, the system will dynamically allocate and reclaim memory as needed. Therefore, there is no need to declare the capacity of the list in advance.

Second, the creation of lists

Any type of object can be stored in the ● list

Two ways to create a list

1. Use square brackets []

L = ['python',19,True] print (L) [' python',19,True]

Empty list creation

L = []

2. The construction method of using list

L = list (range (0p8pm 2)) print (L) [0,2,4,6]

Creation of empty list

L = list () print (L) []

III. Additions, deletions, modifications and queries of the list

1. List lookup operation

A), index of the elements in the list

Each element in the list has two indexes of integer type

0 1 2 3 4 positive index

-

| | a | b | c | d | e | list |

-

-5-4-3-2-1 negative index

B), get the index of the specified element in the list

● index method

Finds the specified index of the element, and throws a value error if the index does not exist. You can specify the start and stop of the lookup index.

L = [print (L.index (2)) # 1print (L.index (6)) # 2 the same element returns the index of the first element print (L.index (11)) # ValueError: 11 if is not in list does not exist, a value error print (L.index) # 1 looks up the index of element 2, starting with index 0 and ending with 5. But it does not include 5print (L.index (6p3j 6)) # 5 View the index of element 6, starting with index 0 and ending with 6, but excluding 6

C), use [n] to get the elements in the list, but only one element at a time

L = [1, 2, 6, 9, 4, 6, 7] print (L [0]) # 1 gets the element print (L [- 3]) # 4 with index 0 and gets the element print (L [- 8]) # IndexError: list index out of range with index-3 if the index does not exist, the index is out of bounds.

D), use elements in slices or lists to get multiple elements at a time

● syntax format [start:stop:step]

1. The resulting slice is still a list and a copy of the original list fragment.

2. The resulting slice does not include the elements corresponding to the index stop

3. If step is not specified, the default value is 1, and the syntax format can be simplified to [start:stop].

4. When step is positive

If you do not specify start, the first element of the slice defaults to the first element of the list

If you do not specify stop, the last element of the slice defaults to the last element of the list

Calculate slices from index start

5. When step is negative

If you do not specify start, the first element of the slice defaults to the last element of the list

If you do not specify stop, the last element of the slice defaults to the first element of the list

Calculate slices forward from index start

L = [5 print 6:-1] # [4, 8, 9, 3, 3, 6, 5] list all values print (L [::-1]) # [4, 8, 9, 1, 3, 6, 5] list all values reverse print (L [: 3Rue 1]) # [4, 8, 9] step is negative, do not specify start Is the last element, stop is 3 is index 3, step size is 1print (L [- 3 step step is negative) # [9, 1, 3, 6, 5] if stop is not specified, start is the value corresponding to index-3, step size is 1

● index slices are allowed to be out of bounds

L = [5, 6, 3, 1, 9, 8, 4] print (L [: 100]) # [5, 6, 3, 1, 9, 8, 4] # starting index is not specified, default is the first element. However, if 100 exceeds the maximum index, print all lists print (L [- 100:]) # [5, 6, 3, 1, 9, 8, 4] # end index is not specified, default is the last element, the starting index is-100 (- 7 last negative index), print all lists

● calls the slice constructor

Slice () list [slice (start,stop,step)] L = [5, 6, 3, 1, 9, 8, 4] print (L [slice (1 slice 5 None,None,None 2)]) # [6, 1] print (L [1:5:2]) # [6, 1] print (L [slice (None,None,None)]) # [5, 6, 3, 1, 9, 8, 4], default is Noneprint (L [:]) # [5, 6, 3, 1, 9, 8, 4]

E), 'in' or' not in' checks whether the element exists in the list

L = [5, 6, 3, 1, 9, 8, 4] inprint (3 in L) # exists as TrueTrueprint (11 in L) # does not exist as FalseFalsenot inprint (1 not in L) 1 exists in the list, not in returns as FalseFalseprint (11 not in L) 11 does not exist in the list, in returns as TrueTrue

2. List increase operation

A), using the append () function

'' append () method, add a value at the end''L = [1 append 2 3 append 4] print (L) # [1 2 print (L) # [1 2 print (L) #] L.append ([7 pee 8]) print (L) # [1 rect 2, 3 Magi 4, 5, [7 Ling 8]]

B), use the extend () method to add all elements to the end accordingly

The extend method is called, and the parentheses in list.extend ([]) are square brackets''L = [1 L.extend ([1) 2]) print (L) [1]

C), call the insert method

'call the insert method to insert the value into the specified location''L = [1 len (L) 3 print 4] 1 print (L) # [1 len 6) 3 print 4] # at the end of the list L.insert (len (L), 10) Magazine (L) # [1 Magne6, 2 Magol 3 Magol 4, 10]

D), assign a new value to the specified slice

L = [2pr 3jre 4je 1] L [2:2] = [5je 6] print (L) L = [2Jing 3je 4pm 1] L [len (L):] = [7je 8] print (L)

3. List modification operation

There are two common ways to modify elements in a list

A), assign a new value to the elements of the specified index (modify only one element at a time)

'' change the element value corresponding to index 1 to 9, and modify only one value at a time.''L = [3Magi 2j0je 1je 2je 1] L [1] = 9print (L) [3,9,0JEI 1JI 2jue 1]

B), specify multiple indexes and modify multiple elements at a time

'' modify the value of the element index at 1:4 The corresponding values to be obtained using [] the index values obtained on the left can be different from the values given on the right''L = [3''L = [3 'print 2'] L] L [1:4] = [4 "print 5" 6] L [3, 4 5 "] L [1:4] = [3, 7] the index (L) [3, 3, 7, 2, 1] L [2:3] = [9] 1] L [3, 3, 9, 2] 1]

4. List deletion operation

A), call the method remove to delete only one element at a time, and delete the first one if there is the same element

L = [2 L.remove 3 4 print 5] L.remove (3) print (L)

B), call the method pop to delete only one element of the specified index at a time.

If the specified index does not exist, an index error is thrown and the index is out of bounds. If you do not specify an index that pop deletes, the last element in the list is deleted.

L = [print (L.pop (1)) # bPop actually has the return value print (L) # ['axiomagem'] print (L.pop ()) print (L) # ['axie ()]

C), use the de statement to delete at least one or more elements at a time

L = [1 del 2 3 del 4 5 5 5] print (L) del L [1:3] print (L)

D), assign an empty list to the specified slice and delete at least one or more elements at a time

L = [1 print 2, 3, 4, 5, 6, 7, 8] L [2:3] = [] print (L) # [1, 2, 3, 5, 6, 7, 8] L [3:6] = [] 7, 8] L [3:6] = [] 3, 8]

E), call the method clear to clear the list

L = [1 L.clear 2 3 4 5] L.clear () print (L) # []

Fourth, use addition and multiplication to operate the list

1. Use addition to operate on the list, which will affect the list itself when using the + = operator

L1 = [1mag2] L2 = [3pyrr4] L3 = L1 + L2print (L1Mague L2 L3) # addition between lists L1 = L2 = [1mage2] L1 + = [3mage2] L1 + = [3mage4] print (L1Magi L2) # the use of + = operator will also have an effect on L2 [1mag2, 3mag4] L1 = [1pen2] L2 = L1 [:] L1 + = [3jre 4] print (L1 L2) # [1, 2, 3, 4] [1, 2]

2. Use multiplication to operate on the list, and the use of the * = operator will affect the list itself.

L1 = [1 2print (L1) #] L1 = L2 = [1Pere2] L1 * = 2print (L1 L2) # [1mage2, 1] L1 = [0] L1 * = 5print (L1) # [0,0,0,0,0] can be used to create a list of specified elements

Fifth, list reversal

1. Call the method reverse to reverse all the elements in the list

L = [1, L.reverse 2, 3, 4] print (L)

2. Call the built-in function reversed, and the list reversed by reversed does not change. The return value of reversed is an iterator object.

L = [1 reversed (L) print (L1) # print (list (L1)) # [5, 4, 3, 2, 1]

VI. Sorting of lists

1. Call the method sort ()

# sort (), which affects the list itself, L2 = ['java','python','shell'] L2.sort (key = len) print (L2) # sorts by string length, [' java',' shell', 'python'] L2 = [1 java',' shell',' python'] L2 = [1, 2, 3, 4, 6, 8, 9] L2.sort () print (L2) # [1, 2, 3, 4, 6, 7, 8, 9]

2. Call the built-in function sorted ()

Sorted (), will not affect the list itself L2 = ['java','shell','python'] print (sorted (L2 shell' = str.upper)) # [' java',' python', 'shell'] L2 = [1, 3, 4, 7] print (sorted (L2)) # [1, 2, 3, 4, 6, 7, 9] print (L2) # [1, 2, 3, 4, 4, 2, 6, 8, 9] L2 7] this is the end of the article on "how to use the list of getting started with Python" Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

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