Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中向用户请求列表输入?_Python_Function_Slice - Fatal编程技术网

如何在Python中向用户请求列表输入?

如何在Python中向用户请求列表输入?,python,function,slice,Python,Function,Slice,我定义了一个需要3个参数的函数,其中一个必须是列表。 我发现的一种解决方案只有在列表由整数组成时才有效,但不一定是这样(同一列表中的类型可能不同)。 如何向用户请求列表类型的输入?e、 g.:当输入类似[1,2,3]的内容时,它被视为一个列表?这里有一种方法: $ cat foo.py import sys input1 = sys.argv[1] input2 = sys.argv[2] print('Before\n-------') print('input1:{},type_of_inp

我定义了一个需要3个参数的函数,其中一个必须是列表。 我发现的一种解决方案只有在列表由整数组成时才有效,但不一定是这样(同一列表中的类型可能不同)。
如何向用户请求列表类型的输入?e、 g.:当输入类似[1,2,3]的内容时,它被视为一个列表?

这里有一种方法:

$ cat foo.py
import sys
input1 = sys.argv[1]
input2 = sys.argv[2]
print('Before\n-------')
print('input1:{},type_of_input1:{}'.format(input1,  type(input1)))
print('input2:{},type_of_input2:{}'.format(input2,  type(input2)))
print('After\n-------')
input1 = input1.split(' ')
print('input1:{},type_of_input1:{}'.format(input1,  type(input1)))
print('input2:{},type_of_input2:{}'.format(input2,  type(input2)))
$
执行输出

$ python foo.py 'foo bar' bat
Before
-------
input1:foo bar,type_of_input1:<type 'str'>
input2:bat,type_of_input2:<type 'str'>
After
-------
input1:['foo', 'bar'],type_of_input1:<type 'list'>
input2:bat,type_of_input2:<type 'str'>
$
$python foo.py'foo bar'bat
之前
-------
输入1:foo-bar,输入1的类型为:
输入2:bat,输入2的类型:
之后
-------
输入1:['foo','bar',输入类型1:
输入2:bat,输入2的类型:
$

如果您完全信任用户输入,您可以使用
eval()
。假设用户输入字符串
[1,2,3]

x = input()    # Python 3, use raw_input for Python 2
y = eval(x)    # Dangerous, don't use with untrusted input

print(y)
# [1, 2, 3]

print(len(y))
# 3
更新:

$ cat foo.py
import sys
input1 = sys.argv[1]
input2 = sys.argv[2]
print('Before\n-------')
print('input1:{},type_of_input1:{}'.format(input1,  type(input1)))
print('input2:{},type_of_input2:{}'.format(input2,  type(input2)))
print('After\n-------')
input1 = input1.split(' ')
print('input1:{},type_of_input1:{}'.format(input1,  type(input1)))
print('input2:{},type_of_input2:{}'.format(input2,  type(input2)))
$

ast.literal\u eval
在这里是一个更好的选择

import ast

x = input()    # Python 3, use raw_input for Python 2
y = ast.literal_eval(x)

print(y)
# [1, 2, 3]

print(len(y))
# 3

保持简单安全,使用
输入
并自己将输入转换为列表:

import re
re.sub("[^\w]", " ", input('-->')).split()
-->This is a string of words converted into a list

output: 

['This', 'is', 'a', 'string', 'of', 'words', 'converted', 'into', 'a', 'list']
输入是内置的:

使用:

如果需要用户输入元组的选项(例如通过输入
1,2,3
),请将
tuple
添加到
isinstance

import ast
while True:
    s=raw_input("Enter a list: ")
    s=ast.literal_eval(s)
    if not isinstance(s, (list, tuple)):
        print "Nope! {} is a {}".format(s, type(s)) 
    else:
        break

阅读与问题的关系是什么?问题是:如何向用户请求列表类型的输入?@JohnDebs:“关系是什么…”对于ast,问题的答案很简单。尝试
ast.literal_eval(“[1,2,'3']”)
它将列表解析为列表,整数解析为整数,字符串解析为字符串。任何其他方法都需要做更多的工作(除了使用
eval
但这有其自身的安全问题…
eval
是一个危险的想法。。。只是等待有人输入
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。如果脚本的目的是由作者在本地以交互方式运行,那么它并不比shell提示符更危险。但是对于这种情况,为什么建议使用
eval
而不是
ast.literal\u eval
ast.literal\u eval
显然是更好的选择。你说得对,我想不出有什么理由在上面使用
eval
。我将更新答案,以明确这一点。谢谢