2017-04-06 54 views
0

我想解決如何將數據從一個嵌套列表追加到另一個嵌套列表。我熟悉.append()。我的問題出現了,因爲我希望用戶能夠選擇要附加的數據片段。基於用戶輸入將嵌套列表數據追加到另一個嵌套列表

所以我有這個樣子兩個嵌套表:

list_one = [['example', 'example', '000', 'example'],['example', 'example', '000', 'example']] 

我想是這樣的...... user_input = input("Enter a number: ") 如果用戶輸入1,我想從該嵌套列表中取數據索引並將該數據附加到另一個列表。

我不希望數據被切片。我想整個['example', 'example', '000', 'example']被追加到另一個列表。

+0

我不明白哪部分你沒有得到。你理解'append'方法,那麼是什麼阻止你做'list_out.append(int(user_input))'? – LoicM

+0

假設我在嵌套列表中有多個列表。我希望用戶輸入一個數字,並根據他們輸入的數字,無論列表在嵌套列表中的索引處,我想將整個列表附加到另一個嵌套列表。哈哈聽起來很混亂。如果這樣做沒有意義,我可以嘗試和改正。我有一種感覺,我將需要使用for循環遍歷嵌套列表中的所有項目,我試圖從中獲取數據。請糾正我,如果我錯了,但請 – youngdaggerdick

+0

我不只是試圖追加用戶輸入到嵌套列表。 – youngdaggerdick

回答

0
list_one = [['example', 'example', '000', 'example'], 
      ['example', 'example', '001', 'example']] 
list_out = [] 
user_input = input("Enter a number: ") 
list_out.append(list_one[int(user_input)]) 
+0

@bren謝謝你! – youngdaggerdick

+0

謝謝你也是男人! – youngdaggerdick

+1

@youngdaggerdick考慮接受最佳答案,以便可以關閉問題。歡呼聲 –