我有一個代碼,我需要將3個不同的輸入放入單獨的列表中。目前,我有3名名單設置:如何將python上的輸入分隔到不同的列表中?
A = []
B = []
C = []
我目前也有3個不同的輸入,每一個列表,我希望這些投入組合成一個輸入,用逗號或分號隔開的這個每個因素。
例如:
Apple,365,rope
使用Python,我將如何在輸入各因子分開,以便它們可以被放入不同的列表?
我試過尋找如何分離使用輸入,但這沒有奏效,因爲我不知道輸入是什麼。
我有一個代碼,我需要將3個不同的輸入放入單獨的列表中。目前,我有3名名單設置:如何將python上的輸入分隔到不同的列表中?
A = []
B = []
C = []
我目前也有3個不同的輸入,每一個列表,我希望這些投入組合成一個輸入,用逗號或分號隔開的這個每個因素。
例如:
Apple,365,rope
使用Python,我將如何在輸入各因子分開,以便它們可以被放入不同的列表?
我試過尋找如何分離使用輸入,但這沒有奏效,因爲我不知道輸入是什麼。
A = []
B = []
C = []
# if string
your_input = "Apple,365,rope"
your_input = your_input.split(",")
A = [your_input[0]]
B = [your_input[1]]
C = [your_input[2]]
print A, B, C
# if tuple
your_input = ("Apple", "365" , "rope")
A = [your_input[0]]
B = [your_input[1]]
C = [your_input[2]]
print A, B, C
我會建議不要屏蔽內置的「輸入」。 – TigerhawkT3
是的。修正:-) – Xyrus
假設你的輸入是使用input()
功能在命令行中,你可以做到以下幾點:
A = []
B = []
C = []
# let's say you input "Apple,365,rope"
my_input = input()
# we split it on each commma into a list -> ["Apple", "365","rope"]
split_input_list = myinput.split(',')
# finally we put each input into the respective list
A.append(split_input_list[0])
B.append(split_input_list[1])
C.append(split_input_list[2])
您連續三次分配給'A [0]'。你是不是指'A [0]','B [0]','C [0]'?另外,你必須使用'append()',因爲在空列表中沒有元素'0'。 – TigerhawkT3
@ TigerhawkT3謝謝,copypaste錯誤。固定。 –
什麼結構是輸入?例如,它是一個字符串「Apple,365,rope」,一個元組('Apple',365,'rope')等。「 – Andrew
」將輸入合併爲一個輸入,同時分隔每個因子「沒有任何意義本身。提供任何相關的代碼,以及更多關於你已經嘗試過的內容的解釋,你的輸入是什麼樣的,你的輸出是什麼樣的,什麼決定了輸出的外觀。 – TigerhawkT3