Python 列表 基礎知識

2021-08-26 08:38:13 字數 1026 閱讀 2691

方法一:

方法二:

no_repeat_l = {}.fromkeys(l).keys()
操作一:判斷值是否在/不在列表中in/not in操作符

>>> list1 = [1, 2, 3, 4]

>>> a = 1

>>> a in list1

true

>>> a = 5

>>> a in list1

false

>>> a not

in list1

true

操作二:統計指定值在列表中出現的次數count方法

>>> list1 = [1, 2, 3, 4]

>>> a = 1

>>> list1.count(a)

1

操作三:查詢指定值在列表中出現的位置index方法

index方法返回查詢物件的索引位置,並返回第一次出現的索引,如果沒有找到物件則丟擲異常。

str.index(str, beg=0, end=len(string))

str – 指定檢索的字串

beg – 開始索引,預設為0。

end – 結束索引,預設為字串的長度

舉例:

>>> list1 = [1, 2, 3, 4, 1]

>>> a = 1

>>> list1.index(a)

0>>> list1.index(a, 1)

4>> list1.index(a, 1, 3)

traceback (most recent call last):

file "", line

1, in

valueerror: 1 is not

in list

python基礎知識 列表

1.新增操作 生成乙個新的列表 extend 接受引數並將該引數的每個元素都新增到原有的列表中,原地修改列表而不是新建列表 insert 插入任意物件到列表中,可以控制插入位置。2.修改 修改列表本身只需要直接賦值操作就行。3.刪除操作 del 我們通過索引刪除指定位置的元素。remove 移除列表...

python 基礎知識 列表

列表就相當於乙個容器,用來存放物件,變數等內容,例如下面幾個列表 list1 list2 list list3 hello 17 true 3.14 list4 hello 17 true print list4 下面講 關於列表的一些操作 print list print list insert ...

python基礎知識 列表和元組

1.序列 python中的序列包含6種,分別是列表,元組,字串,unicode字串,buffer物件,xrange物件。1.1通用序 列操作 包括 索引 index 分片 slicing 加 adding 乘 multiplying 檢查成員資格,計算序列長度,找出最大元素,最小元素。我們用列表來說...