首頁  >  文章  >  後端開發  >  Python3實作列表方法的範例詳解

Python3實作列表方法的範例詳解

黄舟
黄舟原創
2017-10-09 10:38:091515瀏覽

Python3 列表序列是Python中最基本的資料結構,以下這篇文章主要給大家介紹了關於Python3學習筆記之列表方法的相關資料,文中透過範例程式碼介紹的非常詳細,對大家的學習或工作具有一定的參考學習價值,需要的朋友可以參考下。

前言

本文主要介紹給大家介紹了關於Python3列表方法的相關內容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。

1 使用[]或list()建立清單


user = []
user = list()

2 使用list() 可以將其他型別轉換成清單


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

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

3 使用[offset ]取得元素或修改元素


>>> 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[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 使用append()新增元素至尾部

形式如: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 使用extend()或+=合併清單

形式如:list1.extend(list2)

這兩個方法都會直接修改原始清單


>>> 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 使用insert()在指定位置插入元素

#形式如:list.insert(offset, item)

insert也不存在越界的問題,偏移量正負都行,越界之後會自動伸縮到界限之內,並不會報錯


>>> 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 使用del刪除某個元素

#形式如:del list[offset]

del是python的語句,而不是列表的方法,del刪除不存在的元素時,也會提示越界


>>> 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 使用remove刪除具有指定值的元素

形式如: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 使用pop()方式傳回某個元素後,並在陣列中刪除它

形式如:list.pop(offset=-1) 偏移量預設等於-1,也就是刪除最後的元素


#
>>> 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使用index()查詢具有特定值的元素位置

形式如: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 使用in判斷值是否存在清單

形式如: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 使用count()記錄特定值出現的次數

#為: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 使用join()將清單轉為字串

形式如: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 使用sort( )重新排列清單元素

形式如: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 使用reverse()將清單翻轉

#形式如: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 使用copy()複製清單

#形式如:list2 = list1.copy()

list2 = list1 這種並不是清單的複製,只是為清單取了別名。實際上還是指向同一個值。


>>> 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 使用clear()清除清單

形式如: 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 使用len()取得清單長度

##形式如:

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 關於列表越界的深入思考

寫了這些方法後,我有一些疑問,為什麼有些操作會提示越界,有些則不會呢?

會提示偏移越界的操作有

  • list[offset] 讀取或修改某個元素

  • #del list[offset] 刪除指定位置的元素

  • list.remove(value) 刪除指定值的元素

  • ##list.pop(offset)刪除指定位置的元素

如果偏移越界,這些方法會錯誤的。我的個人理解是:


假如我想讀取偏移量為10的元素,但是該元素並不存在,如果系統自動給你讀取了列表的最後一個元素,而且不報錯,這是無法容忍的bug。 如果我想刪除第10個元素,但是第10個元素並不存在,而係統幫你刪除了清單的最後一個元素,我覺得這也是無法容忍的。

所以在使用這些方法時,請務必確認該偏移量的元素是否存,否則可能會報錯。

總結#

以上是Python3實作列表方法的範例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn