Turning Input into a list

时间:2015-11-12 16:12:22

标签: python python-2.7 python-3.x

Write a Python program which accepts a sequence of comma-separated numbers from user and generate a list and a tuple with those numbers.

values = input("Input some comma separated numbers : ")

list = values.split(",")

tuple = tuple(list)
print('List : ',list)


print('Tuple : ',tuple)

This does work but is there any other easier way?

2 个答案:

答案 0 :(得分:0)

如果您正在寻找更有效的方法,请查看以下问题: Most efficient way to split strings in Python

如果您正在寻找更清晰或更简洁的方式,这实际上非常简单。我会避免使用" tuple"和"列表"但是,作为变量名称,将变量命名为其类型是不好的做法。

答案 1 :(得分:0)

嗯,您编写的代码非常简洁,但您可以使用以下代码删除更多行:

values = input("Enter some numbers:\n").split(",")
print(values) #This is the list
print(tuple(values)) #This is the tuple