日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python3 从尾部读取_Python3基础:列表详解

發布時間:2023/12/15 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3 从尾部读取_Python3基础:列表详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

列表是最常用的Python數據類型,作為一個方括號內的逗號分隔值出現。

列表的數據項不需要具有相同的類型 ?創建一個列表,只要把逗號分隔的不同的數據項使用方括號括起來即可

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 "", line 1, in

IndexError: list index out of range

>>> users[-100]

Traceback (most recent call last):

File "", line 1, in

IndexError: list index out of range

# 修改某個元素

>>> users[0] = 'wdd'

>>> users

['wdd', 'b', 'c', 'd', 'e']

>>>

4 列表切片與提取元素

列表的切片或者提取之后仍然是一個列表

形如:list[start:end:step]>>> users

['wdd', 'b', 'c', 'd', 'e']

# 正常截取 注意這里并不會截取到users[2]

>>> users[0:2]

['wdd', 'b']

# 也可從尾部截取

>>> users[0:-2]

['wdd', 'b', 'c']

# 這樣可以獲取所有的元素

>>> users[:]

['wdd', 'b', 'c', 'd', 'e']

# 也可以加上步長參數

>>> users[0:4:2]

['wdd', 'c']

# 也可以通過這種方式去將列表取反

>>> users[::-1]

['e', 'd', 'c', 'b', 'wdd']

# 注意切片時,偏移量可以越界,越界之后不會報錯,仍然按照界限來處理 例如開始偏移量如果小于0,那么仍然會按照0去計算。

>>> users

['wdd', 'b', 'c', 'd', 'e']

>>> users[-100:3]

['wdd', 'b', 'c']

>>> users[-100:100]

['wdd', 'b', 'c', 'd', 'e']

>>>

5 使用append()添加元素至列表尾部

形如:list.append(item)>>> users

['wdd', 'b', 'c', 'd', 'e']

>>> users.append('ddw')

>>> users

['wdd', 'b', 'c', 'd', 'e', 'ddw']

6 使用extend()或+=合并列表

形如:list1.extend(list2)

這兩個方法都會直接修改原列表>>> users

['wdd', 'b', 'c', 'd', 'e', 'ddw']

>>> names

['heihei', 'haha']

>>> users.extend(names)

>>> users

['wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha']

>>> users += names

>>> users

['wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', 'haha']

7 使用insert()在指定位置插入元素

形如:list.insert(offset, item)

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

['wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', 'haha']

>>> users.insert(0,'xiaoxiao')

>>> users

['xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', 'haha']

>>> users.insert(-1,'-xiaoxiao')

>>> users

['xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha']

# 下面-100肯定越界了

>>> users.insert(-100,'-xiaoxiao')

>>> users

['-xiaoxiao', 'xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha']

# 下面100也是越界了

>>> users.insert(100,'-xiaoxiao')

>>> users

['-xiaoxiao', 'xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']

8 使用del刪除某個元素

形如:del list[offset]

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

['-xiaoxiao', 'xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']

>>> del users[0]

>>> users

['xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']

>>> del users[100]

Traceback (most recent call last):

File "", line 1, in

IndexError: list assignment index out of range

>>> del users[-100]

Traceback (most recent call last):

File "", line 1, in

IndexError: list assignment index out of range

9 使用remove刪除具有指定值的元素

形如:list.remove(value)>>> users

['xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']

# 刪除指定值'c'

>>> users.remove('c')

>>> users

['xiaoxiao', 'wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']

# 刪除不存在的值會報錯

>>> users.remove('alsdkfjalsdf')

Traceback (most recent call last):

File "", line 1, in

ValueError: list.remove(x): x not in list

# 如果該值存在多個,那么只能刪除到第一個

>>> users.remove('haha')

>>> users

['xiaoxiao', 'wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']

10 使用pop()方式返回列表某個元素后,并在數組里刪除它

形如:list.pop(offset=-1) 偏移量默認等于-1,也就是刪除最后的元素>>> users

['xiaoxiao', 'wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']

# 刪除最后的元素

>>> users.pop()

'-xiaoxiao'

>>> users

['xiaoxiao', 'wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']

# 如果列表本身就是空的,那么pop時會報錯

>>> user.pop(0)

Traceback (most recent call last):

File "", line 1, in

IndexError: pop from empty list

>>> users.pop(0)

'xiaoxiao'

>>> users

['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']

# 越界時也會報錯

>>> users.pop(100)

Traceback (most recent call last):

File "", line 1, in

IndexError: pop index out of range

11 使用index()查詢具有特定值的元素位置

形如:list.index(value)# index只會返回第一遇到該值得位置

>>> users

['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']

>>> users.index('heihei')

5

# 如果該值不存在,也會報錯

>>> users.index('laksdf')

Traceback (most recent call last):

File "", line 1, in

ValueError: 'laksdf' is not in list

12 使用in判斷值是否存在于列表

形如:value in list>>> users

['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']

>>> 'wdd' in users

True

13 使用count()記錄列表中特定值出現的次數

形如:list.count(value)>>> users

['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']

>>> users.count('heihei')

2

>>> users.count('h')

0

14 使用join()將列表轉為字符串

形如:string.join(list)>>> users

['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']

>>> ','.join(users)

'wdd,b,d,e,ddw,heihei,heihei,-xiaoxiao,haha'

>>> user

[]

>>> ','.join(user)

''

15 使用sort()重新排列列表元素

形如:list.sort()>>> users

['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']

# 默認是升序排序

>>> users.sort()

>>> users

['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd']

# 加入reverse=True, 可以降序排序

>>> users.sort(reverse=True)

>>> users

['wdd', 'heihei', 'heihei', 'haha', 'e', 'ddw', 'd', 'b', '-xiaoxiao']

# 通過匿名函數,傳入函數進行自定義排序

>>> students

[{'name': 'wdd', 'age': 343}, {'name': 'ddw', 'age': 43}, {'name': 'jik', 'age': 90}]

>>> students.sort(key=lambda item: item['age'])

>>> students

[{'name': 'ddw', 'age': 43}, {'name': 'jik', 'age': 90}, {'name': 'wdd', 'age': 343}]

>>> students.sort(key=lambda item: item['age'], reverse=True)

>>> students

[{'name': 'wdd', 'age': 343}, {'name': 'jik', 'age': 90}, {'name': 'ddw', 'age': 43}]

>>>

16 使用reverse()將列表翻轉

形如:list.reverse()>>> users

['wdd', 'heihei', 'heihei', 'haha', 'e', 'ddw', 'd', 'b', '-xiaoxiao']

>>> users.reverse()

>>> users

['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd']

17 使用copy()復制列表

形如:list2 = list1.copy()

list2 = list1 這種并不是列表的復制,只是給列表起了別名。實際上還是指向同一個值。>>> users

['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd']

>>> users2 = users.copy()

>>> users2

['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd']

>>>

18 使用clear()清空列表

形如: list.clear()>>> users2

['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd']

>>> users2.clear()

>>> users2

[]

19 使用len()獲取列表長度

形如:len(list)>>> users

['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd']

>>> len(users)

9

提示偏移量越界的操作有list[offset] 讀取或者修改某個元素

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

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

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

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的python3 从尾部读取_Python3基础:列表详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。