Python基礎-list的各種操作


以下是list數據類型的各種操作

list. append (x)    給list末尾添加一個元素

Add an item to the end of the list; equivalent to a[len(a):] [x].

list. extend (L)    添加一組數據到list 的末尾

Extend the list by appending all the items in the given list; equivalent to a[len(a):] L.

list. insert (ix) 在指定位置插入一個數據

Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).

list. remove (x)   刪除list的第一個x數據

Remove the first item from the list whose value is x. It is an error if there is no such item.

list. pop ([i])     刪除list中指定索引位置的元素,如果不加索引【list.pop()】,則刪除的是最后一個元素

Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)

list. index (x)  得到list中的元素對應的索引

Return the index in the list of the first item whose value is x. It is an error if there is no such item.

list. count (x)  統計list中的元素的個數

Return the number of times x appears in the list.

list. sort (cmp=Nonekey=Nonereverse=False)

Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).

list. reverse ()

Reverse the elements of the list, in place.

You might have noticed that methods like insertremove or sort that only modify the list have no return value printed – they return the default None. This is a design principle for all mutable data structures in Python.

你可能注意到了,像insert,remove和sort方法只是改變了list的數值而沒有輸出,因為他們的返回值送None,這是Python中所有可變數據結構的設計原則。

例如

a = [1, 2, 333, -1, 333, 13.89, 'Test']
a.append(4)
# [1, 2, 333, -1, 333, 13.89, 'Test', 4]
a.extend(range(1, 11))
# [1, 2, 333, -1, 333, 13.89, 'Test', 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a.count(333)
# 2
a.insert(1, 'a')
# [1, 'a', 2, 333, -1, 333, 13.89, 'Test', 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a.remove(1)
# ['a', 2, 333, -1, 333, 13.89, 'Test', 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a.pop()
# ['a', 2, 333, -1, 333, 13.89, 'Test', 4, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a.pop(3)
# ['a', 2, 333, 333, 13.89, 'Test', 4, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print a.index(2)
# 1
print a.count(333)
# 2
a.sort()
# [1, 2, 2, 3, 4, 4, 5, 6, 7, 8, 9, 13.89, 333, 333, 'Test', 'a']
a.reverse()
# ['a', 'Test', 333, 333, 13.89, 9, 8, 7, 6, 5, 4, 4, 3, 2, 2, 1]

Using Lists as Stacks(使用list作為棧)

利用list的方法可以簡單的實現棧,由於棧是“后進先出”原則,可以利用append()和pop()方法來模擬棧

stack = [1, 2, 3]
stack.append(4)
stack.append(5)
# [1, 2, 3, 4, 5]
stack.pop()
# [1, 2, 3, 4]

 

Using Lists as Queues(使用list作為隊列)

利用list的方法可以實現隊列,由於隊列是“先進先出原則”,可以利用append()和pop()方法,但是這不是最有效的,因為這樣,每個元素的都有對應的移動一位。要實現一個隊列,請使用collections.deque,它被設計為從兩端快速添加和彈出。

from collections import deque
queue = deque(["Eric", "John", "Michael"])
queue.append("Terry")
queue.append("Graham")
# deque(['Eric', 'John', 'Michael', 'Terry', 'Graham'])
queue.popleft()
queue.popleft()
# deque(['Michael', 'Terry', 'Graham'])

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM