好的,所以我有這個函式,它需要 3 個引數,它們是串列。然后根據將串列輸入函式的順序,函式將它們混合在一起以創建一個新串列。
我還有一個名為“集合”的串列串列,這是我想將我的串列混合在一起的 3 個訂單。
然后我有一個 while 回圈,我將我的集合放入其中。
然而,這就是事情變得棘手的地方。我也有 3 個列印件,當我的回圈運行 3 次時,我只想對每個回圈應用一個列印件。
到目前為止,我已經嘗試將我的列印結果存盤到一個串列中,但由于某些變數只存在于回圈中,所以無法
def mix(a,b,c):
d,e,f = iter(a), iter(b), iter(c)
result = [item for sublist in zip(d,e,f) for item in sublist]
result = [item for item in d]
result = [item for item in e]
result = [item for item in f]
return result
a = ["A","B","C","D","E"]
b = ['a','b','c','d','e']
c = ['1','2','3','4','5']
sets = [[a,b,c],[b,c,a],[c,a,b]]
q = 0
while q < len(sets):
a = sets[q][0]
b = sets[q][1]
c = sets[q][2]
d = a[::-1]
e = b[::-1]
f = c[::-1]
print(mix(a,b,c)) # first loop only
print(mix(d,e,f)) # second loop only
print(mix(a,e,c)) # third loop only
q =1
我也嘗試過添加空白串列,但我沒有達到預期的結果。
def mix(a,b,c):
d,e,f = iter(a), iter(b), iter(c)
result = [item for sublist in zip(d,e,f) for item in sublist]
result = [item for item in d]
result = [item for item in e]
result = [item for item in f]
return result
a = ["A","B","C","D","E"]
b = ['a','b','c','d','e']
c = ['1','2','3','4','5']
d=[]
e=[]
f=[]
sets = [[a,b,c],[b,c,a],[c,a,b]]
outputs = [mix(a,b,c),mix(d,e,f),mix(a,e,c)]
q = 0
while q < len(sets):
a = sets[q][0]
b = sets[q][1]
c = sets[q][2]
d = a[::-1]
e = b[::-1]
f = c[::-1]
print(outputs[q])
q =1
預期輸出:
['A', 'a', '1', 'B', 'b', '2', 'C', 'c', '3', 'D', 'd', '4', 'E', 'e', '5']
['e', '5', 'E', 'd', '4', 'D', 'c', '3', 'C', 'b', '2', 'B', 'a', '1', 'A']
['1', 'E', 'a', '2', 'D', 'b', '3', 'C', 'c', '4', 'B', 'd', '5', 'A', 'e']
uj5u.com熱心網友回復:
正如 Scott Hunter 建議的那樣,您可以將您的 print 陳述句包含在if else其中,而您的 while 回圈變為:
while q < len(sets):
a = sets[q][0]
b = sets[q][1]
c = sets[q][2]
d = a[::-1]
e = b[::-1]
f = c[::-1]
if q == 0:
print(mix(a,b,c)) # first loop only
elif q == 1:
print(mix(d,e,f)) # second loop only
else:
print(mix(a,e,c)) # third loop only
q =1
希望這是預期的輸出:
['A', 'a', '1', 'B', 'b', '2', 'C', 'c', '3', 'D', 'd', '4', 'E', 'e', '5']
['e', '5', 'E', 'd', '4', 'D', 'c', '3', 'C', 'b', '2', 'B', 'a', '1', 'A']
['1', 'E', 'a', '2', 'D', 'b', '3', 'C', 'c', '4', 'B', 'd', '5', 'A', 'e']
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/474782.html
標籤:Python
上一篇:如何在Pandas中檢索前一個紐約證券交易所交易日?
下一篇:這種雙向鏈表搜索代碼如何實作?
