2015-04-28 15 views
-2

我有一个代码,我需要将3个不同的输入放入单独的列表中。目前,我有3名名单设置:如何将python上的输入分隔到不同的列表中?

A = [] 
B = [] 
C = [] 

我目前也有3个不同的输入,每一个列表,我希望这些投入组合成一个输入,用逗号或分号隔开的这个每个因素。

例如:

Apple,365,rope 

使用Python,我将如何在输入各因子分开,以便它们可以被放入不同的列表?

我试过寻找如何分离使用输入,但这没有奏效,因为我不知道输入是什么。

+0

什么结构是输入?例如,它是一个字符串“Apple,365,rope”,一个元组('Apple',365,'rope')等。“ – Andrew

+0

”将输入合并为一个输入,同时分隔每个因子“没有任何意义本身。提供任何相关的代码,以及更多关于你已经尝试过的内容的解释,你的输入是什么样的,你的输出是什么样的,什么决定了输出的外观。 – TigerhawkT3

回答

0
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 
+0

我会建议不要屏蔽内置的“输入”。 – TigerhawkT3

+0

是的。修正:-) – Xyrus

0

假设你的输入是使用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]) 
+0

您连续三次分配给'A [0]'。你是不是指'A [0]','B [0]','C [0]'?另外,你必须使用'append()',因为在空列表中没有元素'0'。 – TigerhawkT3

+0

@ TigerhawkT3谢谢,copypaste错误。固定。 –

相关问题