python 列表問題?

時間 2021-06-02 04:47:18

1樓:鄭少巨集

>>> a = [1,2]

>>> b = [1,2,3,4,5,6,7,8,9,10,11,12]

>>> a in [[i,j] for ia,i in enumerate(b[:-1]) for ib,j in enumerate(b[1:]) if ia==ib ]

2樓:

>>>a=

[1,2

]>>>b=

[1,2

,3,4

,5,6

,7,8

,9,10

,11,12

,13,14

,15,16

,17,18

,19]>>>ain

[b[i

:i+len(a

)]foriin

range

(len(b

)-len(

a))]

True

>>>

3樓:亦念

For the list and tuple types,x in yis true if and only if there exists an indexisuch thatx == y[i]is true.對於列表和元組這兩個資料型別,x in y當且僅當存在乙個索引值i使得x == y[i]時才為真。

見 http://

docs.python.org/reference/expressions.html#in如果不要求順序一致,可以用集合實現:

set(a) <= set(b) # 子集>>> True

set(a) < set(b) # 真子集,即 set(a) <= set(b) and set(a) != set(b)

>>> True

見 http://

docs.python.org/library/stdtypes.html#set-types-set-frozenset

4樓:

list的in, 是指某元素是否在list中比如 [1, 2, 3]

1 in [1, 2, 3]

>>> True

要是獲取相差值

a = [1, 2]

b = [1, 2, 3]

只需要set(a) ^ set(b)

>>> [3]

獲得相同包含的值

set(a) & set(b)

>>> [1, 2]

為什麼python中將列表1賦值給列表2後將列表2順序反轉也會影響列表1?

酒罈壇兒 python中的變數本質都是指標變數,儲存的是資料在記憶體中的位址。所以你通過其中乙個變數對資料本身進行任何操作都會直接影響另乙個變數。 Gavin 其實問題的關鍵在於 第一,賦值其實只是複製了物件的引用,並沒有建立新的物件第二,操作物件是乙個可變物件對於ls1 1,2,3,4,5 語句,...

python如何實現列表交叉合併

蓋世小猛男 all list length1 len res list length2 len res list2 if length1 length2 length length2 elif length1 length2 length length1 else length length1 fo...

python 怎麼把大列表裡的小列表處理掉?

其實把 1,1,1,1,1,1,1,1 2,2,2,2,2,2,2,2 3,3,3,3,3,3,3,3 4,4,4,4,4,4,4,4 這個大list變成 1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4 這樣的小list...