程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

15 extremely simple Python list code, take it and use it!

編輯:Python

Python List knowledge points summary , I concluded that 5 A word : Add, delete and change order .

Is to add new elements to the list 、 Remove elements 、 Change the element 、 Query element 、 Sort the list .

Next, the corresponding expansion is Python exercises .

increase

Add new elements to the list , There are four ways .

1、 use extend Method .

>>> l = [1, 2, 3]
>>> j = [4, 5, 6]
>>> l.extend(j)
>>> l
[1, 2, 3, 4, 5, 6]

2、 Use operators + Splice the list directly .

>>> l = [1, 2, 3]
>>> j = [4, 5, 6]
>>> l + j
[1, 2, 3, 4, 5, 6]

extend Methods and Operator + The effect looks the same , There are differences in principle , You can use the online tool to view the operation principle of the code .

Tools to address :

http://www.pythontutor.com/visualize.html#mode=edit

You can see the schematic diagram of the operation ,extend The method is to directly add... To the original list ,a The list is added directly ,extend Method returns None. And if you add with an operator , The result of the addition opens up a new memory to store the new list .

3、 use insert Method to insert a new object in the corresponding position .

We need to give the index of the insertion position , from 0 Start .

>>> num = [1, 2, 4, 5]
>>> num.insert(2, 'three')
>>> num
[1, 2, 'three', 4, 5]

4、 Insert an object into the list by slicing .

>>> num = [1, 2, 4, 5]
>>> num[2:2] = ['three']
>>> num
[1, 2, 'three', 4, 5]

Delete

1、 We can use pop Method to delete elements from the list .

pop The default method is to delete the last element .

>>> num = [1, 2, 4, 5]
>>> num.pop()
5
>>> num
[1, 2, 4]

We can also specify the index position to delete the corresponding element .

>>> num = [1, 2, 4, 5]
>>> num.pop(1)
2
>>> num
[1, 4, 5]

2、 use remove Method to delete an element .

If you delete more than one element , Only the first one will be deleted , Others will not be deleted .

>>> num = [1, 2, 4, 5, 4]
>>> num.remove(4)
>>> num
[1, 2, 5, 4]

3、 If you need to empty all the list elements , use claer Method .

>>> l = [1, 2, 3]
>>> l.clear()
>>> l
[]

Change

Modify the elements in the list , Let's just replace it with a subscript assignment .

>>> num = [1, 2, 4, 5]
>>> num[1] = 'two'
>>> num
[1, 'two', 4, 5]

check

Query the elements in the list , We also use subscripts to look directly at .

>>> num = [1, 2, 4, 5]
>>> num[0]
1

We can also query the index value corresponding to a specific element , use index Method .

>>> l = ['are', 'you', 'ok']
>>> l.index('you')
1

order

Arrange the elements in the list in ascending and descending order

This can be used sort and sorted Can do it .

What is the difference between the two methods ? We directly use the operating principle tool to view .

sort() Return directly to None, It sorts directly on the original list , Original list changed ,sorted Will open up a new memory space to store the sorted list .

sort and sorted By default, they are arranged in ascending order , If you want to order it in descending order ? That's easy , hold reverse Change the parameter to True That's it , This parameter defaults to False.

Reverse the order of the list elements

This is to turn all the elements in the list around .

1、 use reverse Method .

>>> num = [1, 2, 3, 4, 5]
>>> num.reverse()
>>> num
[5, 4, 3, 2, 1]

2、 Slice it .

>>> num = [1, 22, 45, 99, 49]
>>> num[::-1]
[49, 99, 45, 22, 1]

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved