如何将python上的输入分成不同的列表?

时间:2015-04-28 20:38:22

标签: python list input

我有一个代码,我需要将3个不同的输入放入单独的列表中。目前我设置了3个列表:

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

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

例如:

Apple,365,rope

使用python,我如何将输入中的每个因子分开,以便将它们放入不同的列表中?

我已经尝试过如何使用输入进行分离,但这并没有奏效,因为我不确切知道输入是什么。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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])