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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

list方法

發(fā)布時(shí)間:2025/7/14 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 list方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

查看list的方法

使用dir函數(shù)查看list都有哪些命令可以使用

>>>dir(list) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

list方法

append

>>> list.append.__doc__ 'L.append(object) -- append object to end'

正如doc文件描述的append函數(shù)是用于附加對(duì)象到列表的末尾。

count

>>> list.count.__doc__ 'L.count(value) -> integer -- return number of occurrences of value'

統(tǒng)計(jì)目標(biāo)值在列表中出現(xiàn)的次數(shù)。

extend

?

>>> list.extend.__doc__ 'L.extend(iterable) -- extend list by appending elements from the iterable'

?

在列表的末尾一次性追加另一個(gè)序列的多個(gè)值。

index

>>> list.index.__doc__ 'L.index(value, [start, [stop]]) -> integer -- return first index of value.\nRaises ValueError if the value is not present.'

返回目標(biāo)值第一次出現(xiàn)位置,如果不存在則拋出錯(cuò)誤。可以設(shè)置查找的開始和結(jié)束位置。

insert

>>> list.insert.__doc__ 'L.insert(index, object) -- insert object before index'

在指定位置插入元素

pop

>>> list.pop.__doc__ 'L.pop([index]) -> item -- remove and return item at index (default last).\nRaises IndexError if list is empty or index is out of range.'

移除列表中的一個(gè)元素(默認(rèn)最后一個(gè)),并且返回該元素的值。

remove

>>> list.remove.__doc__ 'L.remove(value) -- remove first occurrence of value.\nRaises ValueError if the value is not present.'

用于移除列表中某個(gè)值的一個(gè)匹配項(xiàng)。

reverse

>>> list.reverse.__doc__ 'L.reverse() -- reverse *IN PLACE*'

將列表中的元素反向存放。

sort

>>> list.sort.__doc__ 'L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;\ncmp(x, y) -> -1, 0, 1'

用于在原位置對(duì)列表進(jìn)行排序。

方法實(shí)例

>>> hello = list('hello') >>> hello ['h', 'e', 'l', 'l', 'o'] >>> hello.append('four') >>> hello ['h', 'e', 'l', 'l', 'o', 'four'] >>> hello.count('four') 1 >>> hello.index('four') 5 >>> b=[1,2,3] >>> hello.extend(b) >>> hello ['h', 'e', 'l', 'l', 'o', 'four', 1, 2, 3] >>> hello.insert(0,'for') >>> hello ['for', 'h', 'e', 'l', 'l', 'o', 'four', 1, 2, 3] >>> hello.pop() 3 >>> hello.pop(0) 'for' >>> hello.remove('four') >>> hello ['h', 'e', 'l', 'l', 'o', 1, 2] >>> hello.reverse() >>> hello [2, 1, 'o', 'l', 'l', 'e', 'h'] >>> hello.sort() >>> hello [1, 2, 'e', 'h', 'l', 'l', 'o'] View Code

?

轉(zhuǎn)載于:https://www.cnblogs.com/codeforever/p/3902262.html

總結(jié)

以上是生活随笔為你收集整理的list方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。