2016-07-28 71 views
-2

我在Python 2所列出:的Python:串聯2列表

list1 [1,1,0,0,1,1,0,0,1,1] 

list2 [a,b,c,d,e,f,-,-,-,-] 

我想輸出如下:

輸出

[a,b,-,-,c,d,-,-,e,f] 

我很失落,試圖多件事情,而不運氣好的話。 任何人都有一個想法如何做到這一點?這裏是我的嘗試:

for e in range (0, len(stringas)): 
    if controllo[e] == "1": 
    memoria.append(stringas[e]) 
    if controllo[e] == "0": 
    memoria.append("-") 
    blocco.append(e) 
    salva.append(stringas[e]) 

print (stringas) 
print (memoria) 

for f in salva: 
    print (blocco[c]) 
    print (salva[c]) 
    memoria.insert(blocco[c], salva[c]) 
    c = c + 1 

在此先感謝您。

+0

你能描述一下你的代碼是應該做的元素,請? – khelwood

+1

請解釋更多的算法來從兩個列表中獲得結果 – Humbalan

+0

我試圖更好地描述它: – ohdecas

回答

0
out = [] 
for i in list1: 
    out.append(list2.pop(0) if i else '-') 
+0

如果您始終有' - '符號,您可以用list2.pop(-1)替換' - ' t列表的末尾2 –

3

試試這個:

output = [list2.pop(b-1) for b in list1] 
0

甚至這一個班輪:

list1 = [1,1,0,0,1,1,0,0,1,1] 

list2 = ['a','b','c','d','e','f','-','-','-','-'] 

output = [list2[i] if v else '-' for i, v in enumerate(list1)] 

print(output) 

UPDATE並不完全是一個襯墊,並可能在for循環更有效的 - 但我更喜歡這種可讀性

list1 = [1,1,0,0,1,1,0,0,1,1] 
list2 = ['a','b','c','d','e','f'] 

output = [] 

j = 0 

for i, v in enumerate(list1): 
    output.append('-' if not v else list2[j]) 
    j = j if not v else j+1 

print(output) 
+0

由於i值遞增,這不起作用。在你的例子中,你會錯過'c','d'這完全不是由OP打算:-) – thiruvenkadam

+0

哦,該死 - 當然會 - 我責備今天早上缺乏咖啡因! –

+0

這樣更好 - 是啊! –

0

根據你的代碼,我認爲這是你想要的。

對於list1中的每個元素,如果元素爲1,則將list2中的下一個元素添加到newList。如果它是0,則將連字符添加到newList。

list1 = [ 0, 0, 1, 1, 1, 0 ] 
list2 = [ 'a', 'b', 'c', 'd', 'e', 'f' ] # idk why you needed hyphens in this list 
newList = [ ] 

i = 0 
j = 0 
for i in range (len(list1)): 
    if list1(i) == 1: 
     newList += [ list2(j) ] 
     j++ 
    if list1(i) == 0: 
     newList += [ '-' ] 
return newList 

# output: [ '-', '-', 'a', 'b', 'c', '-' ] 

除非你的目標是,如果在列表1的元素是1,從列表1開始到newList添加下一個元素,如果它是一個0,從末尾添加下一個元素。這不能保證0創建相應的連字符。

list1 = [ 0, 0, 1, 1, 1, 0 ] 
list2 = [ 'a', 'b', 'c', '-', '-', '-' ] 
newList = [ ] 

i = 0 
start = 0 
end = len(list2) - 1 

for i in range (len(list1)): 
    if list1(i) == 1: 
     newList += [ list2(start) ] 
     start += 1 
    if list1(i) == 0 
     newList += [ list2(end) ] 
     end -= 1 
return newList 

# output: [ '-', '-', 'a', 'b', 'c', '-' ] 
+0

完美的,你進來我的頭,退出恰當的解決方案;) 謝謝你,如何經歷了很多年,你呢? – ohdecas

+0

你想要哪一個?哈哈,很少有經驗... – Yaelle

0

這個程序應該是如下的邏輯: 1.如果List1中的當前元素是1,從列表2 2.從list2中

彈出第一元件如果沒有,彈出的最後一個元素

這裏是您的參考示例代碼:

list1 = [1,1,0,0,1,1,0,0,1,1] 
list2 = ["a","b","c","d","e","f","-","-","-","-"] 
outList = [] 

for index in range(len(list1)): 
    # If the current value of list1 element is 1, pop from the first 
    if list1[index] == 1: 
     outList.append(list2.pop(0)) 
    # If the current value of list1 element is 0, pop from the last 
    else: 
     outList.append(list2.pop(-1)) 

for item in outList: 
    print(item) 
0

我能想到的方案2中描述的輸出如何產生:

1)如果噸他第一次列表中有0,只需添加「 - 」

list1 = [1,1,0,0,1,1,0,0,1,1] 
list2 = ['a', 'b', 'c', 'd', 'e', 'f', '-', '-', '-'] 
index =0 
output = [] 
for element in list1: 
    if element != 0: 
     element = list2[index] 
     index += 1 
    else: 
     element = '-' 
    output.append(element) 

2)如果「0」在列表2表示從結束

list1 = [1,1,0,0,1,1,0,0,1,1] 
list2 = ['a', 'b', 'c', 'd', 'e', 'f', '-', '-', '-'] 
list3 = list2[:] 
output = [] 
for element in list1: 
    if element != 0: 
     element = list3.pop(0) 
    else: 
     element = list3.pop(-1) 
    output.append(element)