如何将输入列表转换为 python 中的字符串
How to convert input lists to string in python
L = [1,2,3]
q = " ".join(str(x) for x in L)
print(q)
# as you see input is = L = [1,2,3]
# when you run the code output = 1 2 3
但是当我想像这样使用这段代码时:
L = input("Enter your lst: ")
q = " ".join(str(x) for x in L)
print(q)
# I enter my input again = [1,2,3]
# and the output is = [ 1 , 2 , 3 ]
# but I was looking for this : 1 2 3
这里有什么问题?我应该怎么做才能获得 True 输出并将输入列表转换为字符串?
我对编码很陌生,所以简单的解释会更好。
input()
returns python 中的一个字符串。您需要将 input()
的结果转换为 list
,然后将其传入。
如果用户输入列表作为逗号分隔值(例如“1,2,3”),那么我们可以像这样解析数据:
inputdata = input("Enter your lst: ")
valueList = [v.strip() for v in inputdata.split(",")]
result = " ".join(valueList)
print(result)
L = [1,2,3]
q = " ".join(str(x) for x in L)
print(q)
# as you see input is = L = [1,2,3]
# when you run the code output = 1 2 3
但是当我想像这样使用这段代码时:
L = input("Enter your lst: ")
q = " ".join(str(x) for x in L)
print(q)
# I enter my input again = [1,2,3]
# and the output is = [ 1 , 2 , 3 ]
# but I was looking for this : 1 2 3
这里有什么问题?我应该怎么做才能获得 True 输出并将输入列表转换为字符串?
我对编码很陌生,所以简单的解释会更好。
input()
returns python 中的一个字符串。您需要将 input()
的结果转换为 list
,然后将其传入。
如果用户输入列表作为逗号分隔值(例如“1,2,3”),那么我们可以像这样解析数据:
inputdata = input("Enter your lst: ")
valueList = [v.strip() for v in inputdata.split(",")]
result = " ".join(valueList)
print(result)