Home  >  Article  >  Backend Development  >  Introduction to common operations on lists in Python

Introduction to common operations on lists in Python

高洛峰
高洛峰Original
2017-03-22 11:16:051779browse

Mainly involving knowledge points

The list is the most common data type in our python. We mainly have the following operations.

1, Index

2, Slice

3, Append

4, Delete

5, Length

6, Loop (also called traversal)

7, Contains

8, Nested

For example Definition list:

List2=['openstack','python','linux',"docker","zabbix","nginx","linux","linux","123","ww33##"]
或者
List2=list(['openstack','python','linux',"docker","zabbix","nginx","linux","linux","123","ww33##"])

Mainly involved basic methods and introduction

Introduction to common operations on lists in Python

Simple example:

1. Definition of list

 >>> List2=list(['openstack','python','linux',"docker","zabbix","nginx","linux","
linux","123","ww33##"])
>>> print(List2)
['openstack', 'python', 'linux', 'docker', 'zabbix', 'nginx', 'linux', 'linux',
'123', 'ww33##']
或者List2=['openstack','python','linux',"docker","zabbix","nginx","linux","
linux","123","ww33##"] 我们直接用后面的这种定义,检验和模拟相关方法和切片等。

2. Slicing and List Value

Slicing is mainly a method to obtain part or a single element in the list. The elements in the list are accessed through subscripts, and the subscripts start counting from 0

Get a value in the list (get the value through the subscript, the subscript starts from 0)

List2=['openstack','python','linux',"docker","zabbix","nginx","linux","linux","123","ww33##"]
print(List2[0])
print(List2[1])
print(List2[-2])
结果:
openstack
python
123
 
    我们发现在取值时下标为零的恰好取的值为是第一位元素,更好的证明了下标从零开始这一说法,
 同时发现print(List2[-2])恰好是倒数第二位元素,由此可见不仅顺着取值,可以倒着取值。
切片:访问里面的前多个值:
List2=['openstack','python','linux',"docker","zabbix","nginx","linux","linux","123","ww33##"]
 
print(List2[0:3]) #取前三个
 
结果:
['openstack', 'python', 'linux']
再试试
print(List2[:3])
结果:
['openstack', 'python', 'linux']
由此我们可以看出开始下标是0的时候,可以省略不写,还需要注意下标[0:3]能取到1,2,3位的元素,
即表示第四位取不到,在我们边界取值时需要注意。
print(List2[3:-1]) #取不到最后一位
print(List2[3:])  #取得第四位到最后一位
print(List2[2:9:2])#取出下标为第二位到下标为9之间的 每隔一个元素取一次
print(List2[-3:-1]) #倒着取值取出倒数第三位和倒数第二位
print(List2[-3:])#取出倒数的后三位
print(List[0::2])#每隔一个元素取一次
print(list[::2]) #每隔一个元素取一次 和上面的一样
结果:
['docker', 'zabbix', 'nginx', 'linux', 'linux', '123']
['docker', 'zabbix', 'nginx', 'linux', 'linux', '123', 'ww33##']
['linux', 'zabbix', 'linux', '123']
['linux', '123']
['linux', '123', 'ww33##']

3. Add elements to the list

Mainly use the append method to append elements , add

List2.append("winner")
print(List2)
结果:
['openstack', 'python', 'linux', 'docker', 'zabbix', 'nginx', 'linux', 'linux', '123', 'ww33##', 'winner']

at the end to insert an element at a certain position, you can use the insert method to achieve

List2.insert(2,"baidu") #在第三位插入元素baidu
print(List2)
结果:
['openstack', 'python', 'baidu', 'linux', 'docker', 'zabbix', 'nginx','123', 'ww33##']
由此看出在某位置插入时,该位置元素整体向后移动。

4. Modify the value of an element

Modify the value of an element, This is generally achieved through reassignment.

List2[2]="我的钢铁"
List2[-1]="人生苦短,我用python!"
#List2.insert(2,"baidu")
print(List2)
['openstack', 'python', '我的钢铁', 'docker', 'zabbix', 'nginx', '123', '人生苦短,我用python!']

5. Delete

Deletion is divided into deleting one and deleting all. When deleting individual items, we can use the remove() method and pop() method, and use del and The clear() method can delete the entire list. Next, we will analyze the differences between the several methods based on examples.

The parameter in the middle of the pop() method is the subscript. When there is no parameter, the last digit is deleted by default.

List2.pop(1)
print(List2)
结果
['openstack', 'linux', 'docker', 'zabbix', 'nginx', '123', 'ww33##']
List2.pop(-4)
print(List2)
结果:
['openstack', 'python', 'linux', 'docker', 'nginx', '123', 'ww33##']

The parameter in the remove() method is directly the content of the element

List2.remove('linux')
print(List2)
结果:
['openstack', 'python', 'docker', 'zabbix', 'nginx', '123', 'ww33##']
当列表存在相同元素linux时:
List2=['openstack','linux','python','linux',"docker","zabbix","nginx","123",'linux',"ww33##"]
List2.remove('linux')
print(List2)
结果:
['openstack', 'python', 'linux', 'docker', 'zabbix', 'nginx', '123', 'linux', 'ww33##']
当存在多个值时只会删除一个值

clear() method is used to process the entire list

List2.clear()
print(List2)
结果:
[]

del can delete the entire list Or a single element

del  List2
print(List2)
del  List[2]
结果:
Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/s14/jiounumber.py", line 21, in <module>
    print(List2)
NameError: name &#39;List2&#39; is not defined
删除个别元素 结合切片
List2=[&#39;openstack&#39;,&#39;云计算&#39;,&#39;python&#39;,"中国","中东",&#39;linux&#39;,123,"ww33##"]
del List2[2]
print(List2)
结果:
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;中国&#39;, &#39;中东&#39;, &#39;linux&#39;, 123, &#39;ww33##&#39;]

Observing the results, you can find that del deletes the defined list and clear() empties the elements of the list.

6. Query the position of an element and the number of occurrences (list elements can be repeated)

Querying the list mainly includes statistics on the length of the list and querying the elements of a certain content At what position, it is mainly implemented using the index() index method and the count() element occurrence method.

List2=[&#39;openstack&#39;,&#39;python&#39;,&#39;linux&#39;,"docker","zabbix","nginx","123","ww33##"]
索引:
print(List2.index("linux"))
结果
2
 
统计:
print(List2.count("linux"))
结果
1
 
List2=[&#39;openstack&#39;,&#39;linux&#39;,&#39;python&#39;,&#39;linux&#39;,"docker","zabbix",&#39;linux&#39;,"nginx","123","ww33##"]
print(List2.index("linux"))
print(List2.count("linux"))
结果:
1
3

It can be seen that when an element contains multiple elements with the same shape, the index usually finds the first element.

7. Flip and sort the list

You can flip and sort the list through the reverse() method and sort() method. The so-called flipping is to flip the list The position of the elements is flipped again, and the sorting is mainly based on the Ascall code.

List2=[&#39;openstack&#39;,&#39;linux&#39;,&#39;python&#39;,&#39;linux&#39;,"docker","zabbix",&#39;linux&#39;,"nginx","123","ww33##"]
List2.reverse()
print(List2)
结果:
[&#39;ww33##&#39;, &#39;123&#39;, &#39;nginx&#39;, &#39;linux&#39;, &#39;zabbix&#39;, &#39;docker&#39;, &#39;linux&#39;, &#39;python&#39;, &#39;linux&#39;, &#39;openstack&#39;]
 
排序:
List2.sort()
print(List2)
[&#39;123&#39;, &#39;docker&#39;, &#39;linux&#39;, &#39;linux&#39;, &#39;linux&#39;, &#39;nginx&#39;, &#39;openstack&#39;, &#39;python&#39;, &#39;ww33##&#39;, &#39;zabbix&#39;]
再稍微复杂一点,里面添加中文和整数等内容
List2=[&#39;openstack&#39;,&#39;云计算&#39;,&#39;python&#39;,123,"docker","zabbix",&#39;linux&#39;,30.84,"123","ww33##"]
List2.sort()
print(List2)
执行结果:
Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/s14/jiounumber.py", line 24, in <module>
    List2.sort()
TypeError: unorderable types: int() < str()
整型和字符串无法比较,接下来,再试试
List2=[&#39;openstack&#39;,&#39;云计算&#39;,&#39;python&#39;,"中国","中东",&#39;linux&#39;,"123","ww33##"]
List2.sort()
print(List2)
[&#39;123&#39;, &#39;linux&#39;, &#39;openstack&#39;, &#39;python&#39;, &#39;ww33##&#39;, &#39;中东&#39;, &#39;中国&#39;, &#39;云计算&#39;]

From this, we find that the sorting of the list is still very NB. Regarding the above integers and strings that cannot be compared, we can use the integer Convert to String comparison.

8. Looping, inclusion and copy(), extend() methods

Looping, usually by for loop, the list will be The method of printing out elements

List2=[&#39;openstack&#39;,&#39;云计算&#39;,&#39;python&#39;,"中国","中东",&#39;linux&#39;,"123","ww33##"]
for  i  in List2 :
    print (i)
print(List2)
结果:
openstack
云计算
python
中国
中东
linux
123
ww33##
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;python&#39;, &#39;中国&#39;, &#39;中东&#39;, &#39;linux&#39;, &#39;123&#39;, &#39;ww33##&#39;]
通过循环我们就可以找出列表的每个元素。同时我们也可以看出这时的打印和之前的有所不同

includes: We can judge whether a certain content is in the list. The return value of the result is a Boolean value Ture or False. If it exists, it is true and if it does not exist, it is false.

List2=[&#39;openstack&#39;,&#39;云计算&#39;,&#39;python&#39;,"中国","中东",&#39;linux&#39;,123,"ww33##"]
print( &#39;linuxww&#39;  in List2)
print(&#39;中东&#39; in List2)
print(123  in List2)
print( 12 in List2)
结果:
False
True
True
False

entend method Using this method, you can merge two lists into one without any impact on one of the values.

List2=[&#39;openstack&#39;,&#39;云计算&#39;,&#39;python&#39;,"中国","中东",&#39;linux&#39;,123,"ww33##"]
List1=[123,&#39;abc&#39;,&#39;中国&#39;]
#print( &#39;linuxww&#39;  in List2)
#print(&#39;中东&#39; in List2)
print(List2,List1)
print(List1)
List1.clear()
print(List1)
结果:
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;python&#39;, &#39;中国&#39;, &#39;中东&#39;, &#39;linux&#39;, 123, &#39;ww33##&#39;] [123, &#39;abc&#39;, &#39;中国&#39;]
[123, &#39;abc&#39;, &#39;中国&#39;]
[]
 
由此发现extend方法并不会影响被合并列表(List1)的值,只有自己做其他操作时才会发生变化。

copy() method

can realize the copying of the list

List2=[&#39;openstack&#39;,&#39;云计算&#39;,&#39;python&#39;,"中国","中东",&#39;linux&#39;,123,"ww33##"]
list3=List2.copy()
print(List2,list3)
结果:
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;python&#39;, &#39;中国&#39;, &#39;中东&#39;, &#39;linux&#39;, 123, &#39;ww33##&#39;]
 [&#39;openstack&#39;, &#39;云计算&#39;, &#39;python&#39;, &#39;中国&#39;, &#39;中东&#39;, &#39;linux&#39;, 123, &#39;ww33##&#39;]
  
注意事项:
 List2=[&#39;openstack&#39;,&#39;云计算&#39;,&#39;python&#39;,["中国","中东"],&#39;linux&#39;,123,"ww33##"]
list3=List2.copy()
print(List2)
print(list3)
List2[2]="MYSQl DBA"
List2[3][1]="北京"
print(List2)
print(list3)
执行结果:
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;python&#39;, [&#39;中国&#39;, &#39;中东&#39;], &#39;linux&#39;, 123, &#39;ww33##&#39;]
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;python&#39;, [&#39;中国&#39;, &#39;中东&#39;], &#39;linux&#39;, 123, &#39;ww33##&#39;]
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;MYSQl DBA&#39;, [&#39;中国&#39;, &#39;北京&#39;], &#39;linux&#39;, 123, &#39;ww33##&#39;]
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;python&#39;, [&#39;中国&#39;, &#39;北京&#39;], &#39;linux&#39;, 123, &#39;ww33##&#39;]
我们将列表List2中的List2[2]的python和List2[3][1]的中东更改为List2[2]="MYSQl DBA"和List2[3][1]="北京
输出后发现复制的那一部分List2[3][1]复制后是一样的,而List2[2]的值会不一样。主要是内存的地址原因。
同时对这种情况想要复制相同的是不行的
List2=[&#39;openstack&#39;,&#39;云计算&#39;,&#39;python&#39;,["中国","中东"],&#39;linux&#39;,123,"ww33##"]
list3=List2
print(List2)
print(list3)
List2[2]="MYSQl DBA"
List2[3][1]="北京"
print(List2)
print(list3)
这里给list3赋值为List2这时会发现更改List2时,会直接更改list3的值
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;python&#39;, [&#39;中国&#39;, &#39;中东&#39;], &#39;linux&#39;, 123, &#39;ww33##&#39;]
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;python&#39;, [&#39;中国&#39;, &#39;中东&#39;], &#39;linux&#39;, 123, &#39;ww33##&#39;]
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;MYSQl DBA&#39;, [&#39;中国&#39;, &#39;北京&#39;], &#39;linux&#39;, 123, &#39;ww33##&#39;]
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;MYSQl DBA&#39;, [&#39;中国&#39;, &#39;北京&#39;], &#39;linux&#39;, 123, &#39;ww33##&#39;]
要想复制完全一样的,我们可以导入copy模块
import copy
List2=[&#39;openstack&#39;,&#39;云计算&#39;,&#39;python&#39;,["中国","中东"],&#39;linux&#39;,123,"ww33##"]
list3=copy.deepcopy(List2)
print(List2)
print(list3)
List2[2]="MYSQl DBA"
List2[3][1]="北京"
print(List2)
print(list3)
执行的结果:
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;python&#39;, [&#39;中国&#39;, &#39;中东&#39;], &#39;linux&#39;, 123, &#39;ww33##&#39;]
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;python&#39;, [&#39;中国&#39;, &#39;中东&#39;], &#39;linux&#39;, 123, &#39;ww33##&#39;]
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;MYSQl DBA&#39;, [&#39;中国&#39;, &#39;北京&#39;], &#39;linux&#39;, 123, &#39;ww33##&#39;]
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;python&#39;, [&#39;中国&#39;, &#39;中东&#39;], &#39;linux&#39;, 123, &#39;ww33##&#39;]
这时发现,List2元素的值改变不会影响list3的值,下面的这种copy,我们称为deep.copy,而列表的
copy()方法是一种浅copy

9. Other operations of the list

In addition to the above main operations, we You can also perform the following operations on the list. The operatorsfor + and * for lists are similar to those for strings. The + sign is used to combine lists, and the * sign is used to repeat lists

List2=[&#39;openstack&#39;,&#39;云计算&#39;,&#39;python&#39;,"中国","中东",&#39;linux&#39;,123]
list3=["open","ip","config"]
print(List2+list3)
print(list3*3 )
结果:
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;python&#39;, &#39;中国&#39;, &#39;中东&#39;, &#39;linux&#39;, 123, &#39;open&#39;, &#39;ip&#39;, &#39;config&#39;]
[&#39;open&#39;, &#39;ip&#39;, &#39;config&#39;, &#39;open&#39;, &#39;ip&#39;, &#39;config&#39;, &#39;open&#39;, &#39;ip&#39;, &#39;config&#39;]
+号实现的功能和之前的append()方式一样,而*表示重复,后面的的数字表示重复的次数。

python listfunction

Above we analyzed the python list method, and there is still a part of the python list List functions, common ones include max(list), min(list), len(list) and list(seq):

len(list) is the length of the list mentioned above,

max(list) The maximum value of the list

min(list) The minimum value of the list

list(seq) Convert tuples to lists, we know the basics of tuplesAttributes are the same as lists, the difference is that lists are defined using [] square brackets, while tuples are defined using () brackets, and the elements of tuples cannot be reassigned.

Example

List2=[&#39;openstack&#39;,&#39;云计算&#39;,&#39;python&#39;,"中国","中东",&#39;linux&#39;,123]
list3=["open","ip","config",&#39;999&#39;,&#39;2929&#39;,]
tet=("www","www",1234)
print(type(tet))
print(list(tet))
print(type(list(tet)))
print(List2+list3)
print(list3*3 )
print(len(list3))
print(max(list3))
print(min(list3))
 
结果:
<class &#39;tuple&#39;>  #元组tet的类型
[&#39;www&#39;, &#39;www&#39;, 1234] #print(list(tet))元组转换列表输出
<class &#39;list&#39;> #print(type(list(tet)))元组转化列表后的类型验证
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;python&#39;, &#39;中国&#39;, &#39;中东&#39;, &#39;linux&#39;, 123, &#39;open&#39;, &#39;ip&#39;, &#39;config&#39;, &#39;999&#39;, &#39;2929&#39;]
[&#39;open&#39;, &#39;ip&#39;, &#39;config&#39;, &#39;999&#39;, &#39;2929&#39;, &#39;open&#39;, &#39;ip&#39;, &#39;config&#39;, &#39;999&#39;, &#39;2929&#39;, &#39;open&#39;, &#39;ip&#39;, &#39;config&#39;, &#39;999&#39;, &#39;2929&#39;]
5 #print(len(list3))列表list3的长度统计
open  #print(max(list3)) 列表的最大值
2929  #print(min(list3)) 列表最小值

10. Nesting of lists

     在上面讲到列表的copy()方法时,我们就用到了列表,因为列表的元素可以是任何类型,所以列表的元素还可以是列表,这时候的列表就会形成嵌套关系。

例如:

List2=[&#39;openstack&#39;,&#39;云计算&#39;,&#39;python&#39;,"中国","中东",[["open","ip"],"config",&#39;999&#39;,&#39;2929&#39;,]]
print(List2)
结果: 实现三层嵌套
[&#39;openstack&#39;, &#39;云计算&#39;, &#39;python&#39;, &#39;中国&#39;, &#39;中东&#39;, [[&#39;open&#39;, &#39;ip&#39;], &#39;config&#39;, &#39;999&#39;, &#39;2929&#39;]]

总结:以上内容主要是python中对列表的操作,全部属于基础知识,并且所有的示例都是亲手实践所得到的结果,由于经验不足,或许只能理解列表知识的一部分,哪里有错误,还请各位朋友指正。

The above is the detailed content of Introduction to common operations on lists in Python. 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