Python3如何實現兩個列表的交叉列印?

時間 2021-06-02 22:37:16

1樓:

defiter_cross(*

iterables

):copy=[

iter

(item

)for

item

initerables

]while

copy

:# 一直迭代到copy列表為空

foritem

incopy

:try

:yield

next

(item

)except

StopIteration

:# 如果next()丟擲StopIeration異常# 那就把item從copy中刪去

copy

.pop

(copy

.index

(item

))上面這個函式可以對任意多個list或tuple交叉迭代,先看看你要求的效果

titlelist=[

'T1'

,'T2'

,'T3'

]urllist=[

'U1'

,'U2'

,'U3'

]foriin

iter_cross

(titlelist

,urllist

):print(i

,end

=' '

)>>>

T1U1

T2U2

T3U3

再舉個演示

import

stringl0=

range(1

,27)l1

=string

.ascii_lowercasel2=

string

.ascii_uppercase

fori

initer_cross(l0

,l1,l2

):print(i

,end

=' '

)>>>1a

A2bB

3cC4

dD5e

E6fF

7gG8

hH9i

I10jJ

11kK12

lL13m

M14nN

15oO16

pP17q

Q18rR

19sS20

tT21u

U22vV

23wW24

xX25y

Y26zZ

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如何實現兩個list按照固定的數字進行交叉列印?

BeyondTheHorizon 運用列表的切片知識點即可完成 newlist list1 list2 list3 print newlist from itertools import zip longest definterlace inputs,k return e forinterlaced...

python3如何進行多行輸入?

奴隸罷了 lines words input 請輸入內容 單獨輸入 w 儲存退出 while words w words input print lines w 字串是停止的字串,而且不會加入到lines列表中.可以用writelines 方法把lines裡的資料寫到檔案裡,Down 勞里林 Raw...