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 understand python list

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

Share

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

This article introduces the relevant knowledge of "how to understand python list". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

directory

List List

1. List creation

2. List access

1) Access to one-dimensional lists

2) Access to two-dimensional lists

3. Modify elements

4. List slices

5, del order

6. List operation

1) List addition

2) List multiplication

7. List method

1) index(value[,start=0[,stop]])

2) count()

3)append()

4)extend()

5)insert()

list1, list creation list1 = [3.14, 1.61, 0, -9, 6]list2 = ['train', 'bus', 'car', 'ship'] list3 = [' a', 200,'b', 150, ' c', 100]list4 = [] #Create empty list

In Python, lists in lists are often used, i.e. two-dimensional lists.

2. List access

Index access applies to objects of all sequence types: lists, tuples, strings.

1) one-dimensional list access vehicle = ['train',' bus','car',' ship'] vehicle[0]'train'vehicle[1]'bus'vehicle[2]'car'2) two-dimensional list access

Access to elements in a two-dimensional list is indicated by two square brackets, the first indicating the selection of a sublist and the second indicating the selection of elements in the selected sublist.

computer=[['IBM',' Apple','Lenovo'],[' America','America',' China']]computer[0][-1]'Lenovo'computer[1][2]'China'3, modify elements4, list slice

1. In a list, you can use the slice operation to select elements at specified positions to form a new list. A simple way to slice is:

Original list name [start : end]

You need to provide a start value and an end value as the start and end index boundaries for the slice.

The elements at the start index are included in the slice, and the elements at the end index are not included in the slice;

Default when slice left index start is 0, default when slice right index end is list length.

This simple slicing operation selects elements from the original list whose indices lie in the interval [start, end) to form a new list.

2. Slice operations can also provide a non-zero integer (i.e., positive or negative, but not zero) as the step value for index value increment. The method of use is:

Original list name [start : end : step]

When the step size is 1, the step parameter can be omitted.

When step is 1, this parameter can be omitted; when step is not 1, this parameter cannot be omitted.

Slice operations work with all sequence types.

5, del order

Use del to delete elements from a list or to delete an entire list.

vehicle = ['train',' bus','car',' ship'] del vehicle[3]vehicle #Removed 'ship'['train',' bus','car'] del vehicle[3] #Out of index range Traceback (most recent call last):File "", line 1, index vehicle[3]IndexError: list assignment index out of rangedel vehicle #Delete list vehicle #List vehicle does not exist Traceback (most recent call last):File "", line 1, vehicleNameError: name 'vehicle' is not defined In addition, remove(), pop(), clear() methods can achieve the deletion of list elements 6, list operations

1) List addition vehicle1 = ['train',' bus','car',' ship'] vehicle2 = ['subway', 'bicycle'] vehicle1 + vehicle2 ['train',' bus','car',' ship','subway', 'bicycle'] vehicle1 #vehicle1 has not changed ['train',' bus','car',' ship'] vehicle2 ['subway','bicycle']vehicle= vehicle1 + vehicle2 #Generate a new list assignment to the variable vehicle ['train',' bus','car',' ship','subway','bicycle']vehicle+=['bike'] #Compound statement assignment vehicle[' train','bus',' car','ship',' subway','bicycle','bike']2) List multiplication vehicle1 = ['train',' bus'] vehicle1 *2['train',' bus','train',' bus'] vehicle1 #Original list remains unchanged ['train',' bus'] vehicle= vehicle1 *2 #Assignment statement vehicle['train',' bus','train',' bus'] vehicle*=2 #Compound assignment statement vehicle['train',' bus','train',' bus','train',' bus','train',' bus'] 7. List method

Methods in lists can be thought of as functions that act on a particular type of Python object, a list.

1) index(value[,start=0[,stop]])

The index() method is used to find the index position of the first element in the list that matches the value.

If no value is specified for the parameter start, the search starts at index 0, otherwise the search starts at index strat.

If there is no value specified for stop, you can find the last element of the list, otherwise you can find it in the index interval located in [start, stop).

If no match is found, an exception is thrown.

vehicle = ['train',' bus','car',' subway','ship',' bicycle','car'] vehicle.index ('car') #index of first occurrence of 'car' in entire list is 22vehicle.index ('plane ') #no'plane'Traceback in entire list (most recent call last):File "", line 1, invehicle.index('plane')ValueError: 'plane' is not in listvehicle.index ('plane ') #There is no'plane'Traceback in the entire list (most recent call last):File "", line 1, invehicle.index ('plane ')ValueError: 'plane' is not in listvehicle.index ('plane') #There is no 'plane'Traceback in the entire list (most recent call last):File "", line 1, invehicle.index('plane')ValueError: 'plane' is not in list2) count()

The count() method counts the number of times an element appears in a list.

vehicle = ['train', 'bus', 'car', 'subway', 'ship', 'bicycle', 'car']vehicle.count('car')2vehicle.count('bus')1vehicle.count('bike')03)append()

append() appends a single element to the end of the list, accepting only one element, the element can be any data type, and the appended element retains the original structure type in the list.

vehicle = ['train',' bus','car',' ship'] vehicle.append ('plane')#appends an element'plane'vehicle[' train','bus',' car','ship',' plane'] vehicle.append(8) #appends an element 8vehicle['train',' bus','car',' ship','plane', 8]vehicle.append([8,9]) #appends an element [8,9]vehicle[' train','bus','car', 'ship',' plane', 8, [8, 9]]vehicle.append(10,11) #appending 2 elements 10 and 11, error Traceback (most recent call last):File "", line 1, vehicle.append(10,11)TypeError: append() takes exactly one argument (2 given)4)extend()

The list.extend() method appends to the end of the list all the elements in the method's parameter container (list, tuple, string, dictionary, collection). If the parameter is a dictionary, all keys in the dictionary are appended.

5)insert()

The insert() method inserts an element into a list at a specified position. The insert method of a list takes two parameters, the first parameter is the index point, the position of the insert, and the second parameter is the inserted element.

"How to understand python list" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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