search
HomeBackend DevelopmentPython TutorialDetailed example of implementing list method in Python3
Detailed example of implementing list method in Python3Oct 09, 2017 am 10:38 AM
python3methodExample

Python3 list sequence is the most basic data structure in Python. The following article mainly introduces you to the relevant information about the list method of Python3 study notes. The article introduces it in great detail through example code, which is very useful for your study or work. It has certain reference and learning value, and friends in need can refer to it.

Preface

This article mainly introduces to you the relevant content about the Python3 list method, and shares it for your reference and study. There is not much to say below. Having said that, let’s take a look at the detailed introduction.

1 Use [] or list() to create a list


##

user = []
user = list()

2 Use list() to convert other types into lists


# 将字符串转成列表
>>> list('abcde')
['a', 'b', 'c', 'd', 'e']

# 将元祖转成列表
>>> list(('a','b','c'))
['a', 'b', 'c']

3 Use [offset ]Get elements or modify elements


>>> users = ['a','b','c','d','e']
# 可以使用整数来获取某个元素
>>> users[0]
'a'
# 可以使用负整数来表示从尾部获取某个元素
>>> users[-1]
'e'

# 数组越界会报错
>>> users[100]
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> users[-100]
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
IndexError: list index out of range

# 修改某个元素
>>> users[0] = &#39;wdd&#39;
>>> users
[&#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;]
>>>

4 List slicing and extracting elements

After slicing or extracting the list, it is still a list


The form is:

list[start:end:step]


>>> users
[&#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;]
# 正常截取 注意这里并不会截取到users[2]
>>> users[0:2]
[&#39;wdd&#39;, &#39;b&#39;]
# 也可从尾部截取
>>> users[0:-2]
[&#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;]
# 这样可以获取所有的元素
>>> users[:]
[&#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;]
# 也可以加上步长参数
>>> users[0:4:2]
[&#39;wdd&#39;, &#39;c&#39;]
# 也可以通过这种方式去将列表取反
>>> users[::-1]
[&#39;e&#39;, &#39;d&#39;, &#39;c&#39;, &#39;b&#39;, &#39;wdd&#39;]

# 注意切片时,偏移量可以越界,越界之后不会报错,仍然按照界限来处理 例如开始偏移量如果小于0,那么仍然会按照0去计算。
>>> users
[&#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;]
>>> users[-100:3]
[&#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;]
>>> users[-100:100]
[&#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;]
>>>

5 Use append() to add elements to the end

The form is like:

list.append( item)


>>> users
[&#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;]
>>> users.append(&#39;ddw&#39;)
>>> users
[&#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;]

6 Use extend() or += to merge lists

Form such as:

list1.extend(list2)

These two methods will directly modify the original list


>>> users
[&#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;]
>>> names
[&#39;heihei&#39;, &#39;haha&#39;]
>>> users.extend(names)
>>> users
[&#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;haha&#39;]
>>> users += names
>>> users
[&#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;haha&#39;, &#39;heihei&#39;, &#39;haha&#39;]

7 Use insert() to insert an element at the specified position

Form:

list.insert(offset, item)

Insert does not have the problem of crossing the boundary. The offset can be positive or negative. After crossing the boundary, it will automatically scale to within the boundary and no error will be reported.


>>> users
[&#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;haha&#39;, &#39;heihei&#39;, &#39;haha&#39;]
>>> users.insert(0,&#39;xiaoxiao&#39;)
>>> users
[&#39;xiaoxiao&#39;, &#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;haha&#39;, &#39;heihei&#39;, &#39;haha&#39;]
>>> users.insert(-1,&#39;-xiaoxiao&#39;)
>>> users
[&#39;xiaoxiao&#39;, &#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;haha&#39;, &#39;heihei&#39;, &#39;-xiaoxiao&#39;, &#39;haha&#39;]
# 下面-100肯定越界了
>>> users.insert(-100,&#39;-xiaoxiao&#39;)
>>> users
[&#39;-xiaoxiao&#39;, &#39;xiaoxiao&#39;, &#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;haha&#39;, &#39;heihei&#39;, &#39;-xiaoxiao&#39;, &#39;haha&#39;]
# 下面100也是越界了
>>> users.insert(100,&#39;-xiaoxiao&#39;)
>>> users
[&#39;-xiaoxiao&#39;, &#39;xiaoxiao&#39;, &#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;haha&#39;, &#39;heihei&#39;, &#39;-xiaoxiao&#39;, &#39;haha&#39;, &#39;-xiaoxiao&#39;]

8 Use del to delete an element

The form is:

del list[offset]

del is a python statement, not a list method. When del deletes non-existent elements, it will also prompt out of bounds


>>> users
[&#39;-xiaoxiao&#39;, &#39;xiaoxiao&#39;, &#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;haha&#39;, &#39;heihei&#39;, &#39;-xiaoxiao&#39;, &#39;haha&#39;, &#39;-xiaoxiao&#39;]
>>> del users[0]
>>> users
[&#39;xiaoxiao&#39;, &#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;haha&#39;, &#39;heihei&#39;, &#39;-xiaoxiao&#39;, &#39;haha&#39;, &#39;-xiaoxiao&#39;]
>>> del users[100]
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> del users[-100]
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range

9 Use remove to delete elements with specified values ​​

Form:

list.remove(value)


>>> users
[&#39;xiaoxiao&#39;, &#39;wdd&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;haha&#39;, &#39;heihei&#39;, &#39;-xiaoxiao&#39;, &#39;haha&#39;, &#39;-xiaoxiao&#39;]
# 删除指定值&#39;c&#39;
>>> users.remove(&#39;c&#39;)
>>> users
[&#39;xiaoxiao&#39;, &#39;wdd&#39;, &#39;b&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;haha&#39;, &#39;heihei&#39;, &#39;-xiaoxiao&#39;, &#39;haha&#39;, &#39;-xiaoxiao&#39;]
# 删除不存在的值会报错
>>> users.remove(&#39;alsdkfjalsdf&#39;)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
# 如果该值存在多个,那么只能删除到第一个
>>> users.remove(&#39;haha&#39;)
>>> users
[&#39;xiaoxiao&#39;, &#39;wdd&#39;, &#39;b&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;heihei&#39;, &#39;-xiaoxiao&#39;, &#39;haha&#39;, &#39;-xiaoxiao&#39;]

10 Use the pop() method to return an element and delete it in the array

The form is:

list.pop(offset=-1) The offset defaults to -1, which means deleting the last element


>>> users
[&#39;xiaoxiao&#39;, &#39;wdd&#39;, &#39;b&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;heihei&#39;, &#39;-xiaoxiao&#39;, &#39;haha&#39;, &#39;-xiaoxiao&#39;]
# 删除最后的元素
>>> users.pop()
&#39;-xiaoxiao&#39;
>>> users
[&#39;xiaoxiao&#39;, &#39;wdd&#39;, &#39;b&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;heihei&#39;, &#39;-xiaoxiao&#39;, &#39;haha&#39;]
# 如果列表本身就是空的,那么pop时会报错
>>> user.pop(0)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
IndexError: pop from empty list
>>> users.pop(0)
&#39;xiaoxiao&#39;
>>> users
[&#39;wdd&#39;, &#39;b&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;heihei&#39;, &#39;-xiaoxiao&#39;, &#39;haha&#39;]
# 越界时也会报错
>>> users.pop(100)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
IndexError: pop index out of range

11 Use index() to query the position of elements with specific values

The form is:

list.index(value)


# index只会返回第一遇到该值得位置
>>> users
[&#39;wdd&#39;, &#39;b&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;heihei&#39;, &#39;-xiaoxiao&#39;, &#39;haha&#39;]
>>> users.index(&#39;heihei&#39;)
5

# 如果该值不存在,也会报错
>>> users.index(&#39;laksdf&#39;)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError: &#39;laksdf&#39; is not in list

12 Use in to determine whether the value exists in the list

Form:

value in list


>>> users
[&#39;wdd&#39;, &#39;b&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;heihei&#39;, &#39;-xiaoxiao&#39;, &#39;haha&#39;]
>>> &#39;wdd&#39; in users
True

13 Use count() to record the number of times a specific value appears

The format is as follows:

list.count(value)


>>> users
[&#39;wdd&#39;, &#39;b&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;heihei&#39;, &#39;-xiaoxiao&#39;, &#39;haha&#39;]
>>> users.count(&#39;heihei&#39;)
2
>>> users.count(&#39;h&#39;)
0

14 Use join() to convert the list to a string

The form is:

string.join(list)


>>> users
[&#39;wdd&#39;, &#39;b&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;heihei&#39;, &#39;-xiaoxiao&#39;, &#39;haha&#39;]
>>> &#39;,&#39;.join(users)
&#39;wdd,b,d,e,ddw,heihei,heihei,-xiaoxiao,haha&#39;
>>> user
[]
>>> &#39;,&#39;.join(user)
&#39;&#39;

15 Use sort( )Rearrange the list elements

in the form:

list.sort()


>>> users
[&#39;wdd&#39;, &#39;b&#39;, &#39;d&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;heihei&#39;, &#39;heihei&#39;, &#39;-xiaoxiao&#39;, &#39;haha&#39;]
# 默认是升序排序
>>> users.sort()
>>> users
[&#39;-xiaoxiao&#39;, &#39;b&#39;, &#39;d&#39;, &#39;ddw&#39;, &#39;e&#39;, &#39;haha&#39;, &#39;heihei&#39;, &#39;heihei&#39;, &#39;wdd&#39;]
# 加入reverse=True, 可以降序排序
>>> users.sort(reverse=True)
>>> users
[&#39;wdd&#39;, &#39;heihei&#39;, &#39;heihei&#39;, &#39;haha&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;d&#39;, &#39;b&#39;, &#39;-xiaoxiao&#39;]

# 通过匿名函数,传入函数进行自定义排序
>>> students
[{&#39;name&#39;: &#39;wdd&#39;, &#39;age&#39;: 343}, {&#39;name&#39;: &#39;ddw&#39;, &#39;age&#39;: 43}, {&#39;name&#39;: &#39;jik&#39;, &#39;age&#39;: 90}]
>>> students.sort(key=lambda item: item[&#39;age&#39;])
>>> students
[{&#39;name&#39;: &#39;ddw&#39;, &#39;age&#39;: 43}, {&#39;name&#39;: &#39;jik&#39;, &#39;age&#39;: 90}, {&#39;name&#39;: &#39;wdd&#39;, &#39;age&#39;: 343}]
>>> students.sort(key=lambda item: item[&#39;age&#39;], reverse=True)
>>> students
[{&#39;name&#39;: &#39;wdd&#39;, &#39;age&#39;: 343}, {&#39;name&#39;: &#39;jik&#39;, &#39;age&#39;: 90}, {&#39;name&#39;: &#39;ddw&#39;, &#39;age&#39;: 43}]
>>>

16 Use reverse() to flip the list

Form:

list.reverse()


>>> users
[&#39;wdd&#39;, &#39;heihei&#39;, &#39;heihei&#39;, &#39;haha&#39;, &#39;e&#39;, &#39;ddw&#39;, &#39;d&#39;, &#39;b&#39;, &#39;-xiaoxiao&#39;]
>>> users.reverse()
>>> users
[&#39;-xiaoxiao&#39;, &#39;b&#39;, &#39;d&#39;, &#39;ddw&#39;, &#39;e&#39;, &#39;haha&#39;, &#39;heihei&#39;, &#39;heihei&#39;, &#39;wdd&#39;]

17 Use copy() to copy the list

Form:

list2 = list1.copy()

list2 = list1 This is not a copy of the list, it just gives the list an alias. In fact, it still points to the same value.


>>> users
[&#39;-xiaoxiao&#39;, &#39;b&#39;, &#39;d&#39;, &#39;ddw&#39;, &#39;e&#39;, &#39;haha&#39;, &#39;heihei&#39;, &#39;heihei&#39;, &#39;wdd&#39;]
>>> users2 = users.copy()
>>> users2
[&#39;-xiaoxiao&#39;, &#39;b&#39;, &#39;d&#39;, &#39;ddw&#39;, &#39;e&#39;, &#39;haha&#39;, &#39;heihei&#39;, &#39;heihei&#39;, &#39;wdd&#39;]
>>>

18 Use clear() to clear the list

The format is as follows:

list.clear()


>>> users2
[&#39;-xiaoxiao&#39;, &#39;b&#39;, &#39;d&#39;, &#39;ddw&#39;, &#39;e&#39;, &#39;haha&#39;, &#39;heihei&#39;, &#39;heihei&#39;, &#39;wdd&#39;]
>>> users2.clear()
>>> users2
[]

19 Use len() to get the length of the list

The form is like:

len(list)


>>> users
[&#39;-xiaoxiao&#39;, &#39;b&#39;, &#39;d&#39;, &#39;ddw&#39;, &#39;e&#39;, &#39;haha&#39;, &#39;heihei&#39;, &#39;heihei&#39;, &#39;wdd&#39;]
>>> len(users)
9

20 In-depth thoughts on list transgression

After writing these methods, I have some questions. Why do some operations prompt out of bounds while others do not?

It will prompt that the offset out-of-bounds operations include

  • list[offset] Reading or modifying an element

  • del list[offset] Delete the element at the specified position

  • list.remove(value) Remove the element with the specified value

  • ##list.pop(offset) Delete the element at the specified position

If the offset is out of bounds, these methods will report an error. My personal understanding is:


If I want to read the element with offset 10, but the element does not exist, if the system automatically reads the last element of the list for you, and If you don't report an error, this is an intolerable bug. If I want to delete the 10th element, but the 10th element does not exist, and the system deletes the last element of the list for you, I think this is intolerable.

So when using these methods, be sure to confirm whether the element at the offset exists, otherwise an error may be reported.

Summarize

The above is the detailed content of Detailed example of implementing list method in Python3. 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
Python函数介绍:exec函数的介绍及示例Python函数介绍:exec函数的介绍及示例Nov 03, 2023 pm 02:09 PM

Python函数介绍:exec函数的介绍及示例引言:在Python中,exec是一种内置函数,它用于执行存储在字符串或文件中的Python代码。exec函数提供了一种动态执行代码的方式,使得程序可以在运行时根据需要生成、修改和执行代码。本文将介绍exec函数的使用方法,并给出一些实际的代码示例。exec函数的使用方法:exec函数的基本语法如下所示:exec

Python函数介绍:abs函数的用法和示例Python函数介绍:abs函数的用法和示例Nov 03, 2023 pm 12:05 PM

Python函数介绍:abs函数的用法和示例一、abs函数的用法介绍在Python中,abs函数是一个内置函数,用于计算给定数值的绝对值。它可以接受一个数字参数,并返回该数字的绝对值。abs函数的基本语法如下:abs(x)其中,x是要计算绝对值的数值参数,可以是整数或浮点数。二、abs函数的示例下面我们将通过一些具体的示例来展示abs函数的用法:示例1:计算

Python函数介绍:sorted函数的功能和示例Python函数介绍:sorted函数的功能和示例Nov 03, 2023 pm 02:47 PM

Python函数介绍:sorted函数的功能和示例Python是一门非常强大的编程语言,拥有丰富的内置函数和模块。在这个系列文章中,我们将逐一介绍Python常用的函数,并提供相应的示例来帮助读者更好地理解和应用这些函数。本篇文章将详细介绍sorted函数的功能和示例。sorted函数用于对可迭代对象进行排序,并返回排序后的新列表。可以用于对数字、字

PHP中endwhile关键字的作用和示例PHP中endwhile关键字的作用和示例Jun 28, 2023 pm 08:00 PM

PHP中endwhile关键字的作用和示例在PHP中,endwhile是一种控制结构,用来实现while循环。它的作用是让程序在满足指定条件的情况下,重复执行一段代码块,直到条件不再满足。endwhile的语法形式如下:while(condition)://循环体代码endwhile;在这个语法中,condition是一个逻辑表达式,当该表达

Python函数介绍:zip函数的介绍及示例Python函数介绍:zip函数的介绍及示例Nov 03, 2023 pm 02:02 PM

Python函数介绍:zip函数的介绍及示例Python是一种高级语言,它提供了许多有用的函数来帮助开发人员快速地编写程序。其中一个函数就是zip函数。Zip函数是Python中的内置函数之一,它可以接受一组可迭代对象(包括列表、元组、集合和字典等),并返回一个由这些可迭代对象中的元素按顺序成对组成的元组。Zip函数可以用于多种情况,例如:1.将两个列表的元

PHP的array_walk()函数介绍及示例使用PHP的array_walk()函数介绍及示例使用Jun 27, 2023 pm 03:31 PM

在PHP中,有很多实用的函数可以帮助我们更方便地处理数组。其中,array_walk()函数就是一个非常实用的函数,它可以对数组中的每个元素进行指定的操作,让我们来了解一下。array_walk()函数介绍array_walk()函数是一个用于处理数组的函数,它的语法结构如下:array_walk(array&$array,callable$callb

PHP中var关键字的作用和示例PHP中var关键字的作用和示例Jun 28, 2023 pm 08:58 PM

PHP中var关键字的作用和示例在PHP中,var关键字用于声明一个变量。以前的PHP版本中,使用var关键字是声明成员变量的惯用方式,现在已经不再推荐使用。然而,在某些情况下,var关键字依然会被使用。var关键字主要用于声明一个局部变量,并且会自动将该变量标记为局部作用域。这意味着该变量仅在当前的代码块中可见,并且不能在其他函数或代码块中访问。使用var

Python函数介绍:__import__函数的用法和示例Python函数介绍:__import__函数的用法和示例Nov 03, 2023 pm 06:15 PM

Python函数介绍:__import__函数的用法和示例Python作为一门高级编程语言,其强大的函数库以及函数的使用方法也是吸引越来越多开发者以及爱好者的原因之一。在Python中,内置的__import__函数是一个非常强大但比较少用的函数,该函数用于动态导入模块。它接收静态的模块名称并返回已导入的模块对象。Syntax:import(name[,

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools