简体   繁体   English

Python 用户输入以列出分隔值

[英]Python User Input To List Separated Values

I've been trying to create a script that:我一直在尝试创建一个脚本:

  • Asks a user to input keywords要求用户输入关键字
  • Stores the user's input in a list将用户的输入存储在列表中
  • Prints each value from the list打印列表中的每个值

When I try to print the list all the values seem to be under the index 0当我尝试打印列表时,所有值似乎都在索引0

inputwords = input('What keywords are you looking for?').split()
inputwordslist = []

inputwordslist.append(inputwords)
inputwordslist = enumerate(inputwordslist)
print (list(inputwordslist))

Output Below:输出如下:

What keywords are you looking for?This is a test
[(0, ['This', 'is', 'a', 'test'])]

For the easiest solution to your problem, @Chris_Rands already posted it in a comment to your question: .split() returns a list.对于您的问题的最简单解决方案,@Chris_Rands 已经将其发布在您问题的评论中: .split()返回一个列表。 You don't have to make a separate one for the result, just enumerate the value returned by the split function:您不必为结果单独制作一个,只需枚举 split 函数返回的值即可:

inputwords = input('What keywords are you looking for?').split()
result = list(enumerate(inputwords))
print(result)

What keywords are you looking for?你在找什么关键词? This is a list of words.这是一个单词列表。
[(0, 'This'), (1, 'is'), (2, 'a'), (3, 'list'), (4, 'of'), (5, 'words.')] [(0, 'This'), (1, 'is'), (2, 'a'), (3, 'list'), (4, 'of'), (5, 'words.')]

As noted in the other answer, it is a good idea to put a space after your prompt, that way there is separation between it and what the user is typing in:正如另一个答案中所指出的,在提示后放置一个空格是个好主意,这样就可以将它与用户输入的内容分开:

inputwords = input('What keywords are you looking for? ').split()

Your code will not work with python2 though, where the input() function is actually running the resulting string through eval() :但是,您的代码不适用于 python2,其中input()函数实际上是通过eval()运行结果字符串:

>>> input()

1 + 2 + 3 1 + 2 + 3
6 6

For more information, see this question .有关更多信息,请参阅此问题

If you want your code to be compatible with both python2 and python3, use this little snippet:如果您希望您的代码与 python2 和 python3 兼容,请使用这个小片段:

try:                       
    input = raw_input      
except NameError:          
    pass                   

This will make sure that input is pointing to the python3 version of the function.这将确保input指向函数的 python3 版本。

The result of input(...).split() is a list. input(...).split()一个列表。 Therefore all you need is:因此,您只需要:

inputwords = input('What keywords are you looking for?').split()
print(list(enumerate(inputwords)))

Note that while this works in Python 3, in Python 2 you would have to use the raw_input() function instead - input expects a Python expression, and returns the result of evaluating that expression.请注意,虽然这在 Python 3 中有效,但在 Python 2 中,您必须使用raw_input()函数 - input需要 Python 表达式,并返回计算该表达式的结果。

>>> inputwords = input('What keywords are you looking for?').split()
What keywords are you looking for?This, that and the other
>>> print(list(enumerate(inputwords)))
[(0, 'This,'), (1, 'that'), (2, 'and'), (3, 'the'), (4, 'other')]

It's helpful to put a space at the end of your prompt string, to clearly separate the prompt and the input.在提示字符串的末尾放置一个空格会很有帮助,以清楚地分隔提示和输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM