Python 获取用户输入的数字列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4663306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:48:49  来源:igfitidea点击:

Get a list of numbers as input from the user

pythonlistraw-input

提问by Underyx

I tried to use raw_input()to get a list of numbers, however with the code

我试图用来raw_input()获取数字列表,但是使用代码

numbers = raw_input()
print len(numbers)

the input [1,2,3]gives a result of 7, so I guess it interprets the input as if it were a string. Is there any direct way to make a list out of it? Maybe I could use re.findallto extract the integers, but if possible, I would prefer to use a more Pythonic solution.

输入[1,2,3]的结果为7,所以我猜它会将输入解释为字符串。有什么直接的方法可以从中列出清单吗?也许我可以re.findall用来提取整数,但如果可能的话,我更愿意使用更 Pythonic 的解决方案。

采纳答案by greentec

In Python 3.x, use this.

在 Python 3.x 中,使用它。

a = [int(x) for x in input().split()]

Example

例子

>>> a = [int(x) for x in input().split()]
3 4 5
>>> a
[3, 4, 5]
>>> 

回答by Sven Marnach

It is much easier to parse a list of numbers separated by spaces rather than trying to parse Python syntax:

解析由空格分隔的数字列表比尝试解析 Python 语法要容易得多:

Python 3:

蟒蛇3:

s = input()
numbers = list(map(int, s.split()))

Python 2:

蟒蛇2:

s = raw_input()
numbers = map(int, s.split())

回答by Karl Knechtel

eval(a_string)evaluates a string as Python code. Obviously this is not particularly safe. You can get safer (more restricted) evaluation by using the literal_evalfunction from the astmodule.

eval(a_string)将字符串计算为 Python 代码。显然这不是特别安全。通过使用模块中的literal_eval函数,您可以获得更安全(更受限制)的评估ast

raw_input()is called that in Python 2.x because it gets raw, not "interpreted" input. input()interprets the input, i.e. is equivalent to eval(raw_input()).

raw_input()在 Python 2.x 中被称为是因为它是原始的,而不是“解释的”输入。input()解释输入,即相当于eval(raw_input())

In Python 3.x, input()does what raw_input()used to do, and you must evaluate the contents manually if that's what you want (i.e. eval(input())).

在 Python 3.x 中,input()做以前raw_input()做的事情,如果这是您想要的(即eval(input())),您必须手动评估内容。

回答by Sean Vieira

You can use .split()

您可以使用 .split()

numbers = raw_input().split(",")
print len(numbers)

This will still give you strings, but it will be a list of strings.

这仍然会给你字符串,但它将是一个字符串列表。

If you need to map them to a type, use list comprehension:

如果需要将它们映射到类型,请使用列表理解:

numbers = [int(n, 10) for n in raw_input().split(",")]
print len(numbers)

If you want to be able to enter in anyPython type and have it mapped automatically andyou trust your users IMPLICITLYthen you can use eval

如果你希望能够在进入任何Python类型,并自动映射你信任你的用户的隐式,那么你可以使用eval

回答by Hans

Try this:

尝试这个:

numbers = raw_input()
numberlist = list(numbers)

回答by Logan

you can pass a string representation of the list to json:

您可以将列表的字符串表示传递给 json:

import json

str_list = raw_input("Enter in a list: ")
my_list = json.loads(str_list)

user enters in the list as you would in python: [2, 34, 5.6, 90]

用户像在 python 中一样输入列表: [2, 34, 5.6, 90]

回答by Achintha Avin

Answer is trivial. try this.

答案是微不足道的。尝试这个。

x=input()

x=input()

Suppose that [1,3,5,'aA','8as']are given as the inputs

假设[1,3,5,'aA','8as']作为输入给出

print len(x)

print len(x)

this gives an answer of 5

这给出了 5 的答案

print x[3]

print x[3]

this gives 'aA'

这给 'aA'

回答by WolfGangM

You can use this function (with int type only) ;)

您可以使用此函数(仅限 int 类型);)

def raw_inputList(yourComment):
     listSTR=raw_input(yourComment)     
     listSTR =listSTR[1:len(listSTR)-1]
     listT = listSTR.split(",")
     listEnd=[]
     for caseListT in listT:
          listEnd.append(int(caseListT))
     return listEnd

This function return your list (with int type) !

此函数返回您的列表(int 类型)!

Example :

例子 :

yourList=raw_inputList("Enter Your List please :")

If you enter

如果你输入

"[1,2,3]" 

then

然后

yourList=[1,2,3]          

回答by xarvier

try this one ,

试试这个,

n=int(raw_input("Enter length of the list"))
l1=[]
for i in range(n):
    a=raw_input()
    if(a.isdigit()):
        l1.insert(i,float(a)) #statement1
    else:
        l1.insert(i,a)        #statement2

If the element of the list is just a number the statement 1 will get executed and if it is a string then statement 2 will be executed. In the end you will have an list l1 as you needed.

如果列表的元素只是一个数字,则语句 1 将被执行,如果它是一个字符串,则语句 2 将被执行。最后,您将根据需要获得一个列表 l1。

回答by Jutta

You just need to typeraw_input().split()and the default split() is that values are split by a whitespace.

您只需要输入raw_input().split(),默认的 split() 是值被一个空格分割。