如何迭代兩個串列,使得輸出串列應將第一個串列的第一個值作為第一個元素,將第二個串列的第一個值作為最后一個元素,將第一個串列的第二個值作為第二個元素,第二個串列的第二個值作為倒數第二個元素,依此類推,然后洗掉重復項。例如:a=['A','C','B','E','D'] b=['B','D','A','E','C']
輸出:c=['A','C','E','D','B']
uj5u.com熱心網友回復:
這是一個可能的解決方案:
from itertools import zip_longest
lst = [[], []]
s = set()
for t in zip_longest(a, b):
for i, x in enumerate(t):
if x is not None and x not in s:
lst[i].append(x)
s.add(x)
c = lst[0] lst[1][::-1]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/435240.html
上一篇:如何在Python回圈中增加索引
下一篇:selenium.common.exceptions.InvalidArgumentException:訊息:在遍歷url串列并作為引數傳遞給get()時引數無效
