當我練習 Python 時,我有兩個串列:
list_a = [1, 'a', 'c', 'e', 'f']
list_b = [2, 'b', 'c', 'd', 'e']
我想要的輸出是:
list_c = [3, 'a','b','c','d','e','f']
我試過:
list_c = [x y for (x, y) in zip(list_a, list_b)]
輸出是:
[3, 'ab', 'cc', 'ed', 'fe']
我也試過:
list_c = set(list_a list_b)
輸出是:
{1, 2, 'a', 'b', 'c', 'd', 'e', 'f'}
有人可以知道該怎么做嗎?真正的輸出是這樣的:
list_c = [3, 'a','b','c','d','e','f']
謝謝。
uj5u.com熱心網友回復:
這是您示例的一個選項,但我不確定您想要什么。
list_a = [1, 'a', 'c', 'e', 'f']
list_b = [2, 'b', 'c', 'd', 'e']
def merge(a,b):
result=[]
for (r,p) in zip(a,b):
if(type(r) == type(p)):
if type(r)==int:
result.append(str(r p))
else:
result.append(r)
result.append(p)
else:
result.append(r)
result.append(p)
result = list(set(result))
result.sort()
for n,k in enumerate(result):
try:
result[n] = int(k)
except:
pass
return(result)
print(merge(list_a,list_b))
印刷:[3, 'a', 'b', 'c', 'd', 'e', 'f']
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/414104.html
標籤:
