Get a list of numbers as input from the user

試著忘記壹切 提交于 2019-11-25 22:26:47

问题


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

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.findall to extract the integers, but if possible, I would prefer to use a more Pythonic solution.


回答1:


In Python 3.x, use this.

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

Example

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



回答2:


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

Python 3:

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

Python 2:

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



回答3:


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_eval function from the ast module.

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()).

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())).




回答4:


You can use .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 any Python type and have it mapped automatically and you trust your users IMPLICITLY then you can use eval




回答5:


Another way could be to use the for-loop for this one. Let's say you want user to input 10 numbers into a list named "memo"

memo=[] 
for i in range (10):
    x=int(input("enter no. \n")) 
    memo.insert(i,x)
    i+=1
print(memo) 



回答6:


num = int(input('Size of elements : '))
arr = list()

for i in range(num) :
  ele  = int(input())
  arr.append(ele)

print(arr)



回答7:


you can pass a string representation of the list to 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]




回答8:


a=[]
b=int(input())
for i in range(b):
    c=int(input())
    a.append(c)

The above code snippets is easy method to get values from the user.




回答9:


Answer is trivial. try this.

x=input()

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

print len(x)

this gives an answer of 5

print x[3]

this gives 'aA'




回答10:


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.




回答11:


Get a list of number as input from the user.

This can be done by using list in python.

L=list(map(int,input(),split()))

Here L indicates list, map is used to map input with the position, int specifies the datatype of the user input which is in integer datatype, and split() is used to split the number based on space.

.




回答12:


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

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) !

Example :

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

If you enter

"[1,2,3]" 

then

yourList=[1,2,3]          



回答13:


In Python 3 :

input_list = [int(x.strip()) for x in input("enter list:").strip()[1:-1].split(",")]

It will ask to "enter list" so just enter list like [2,4,5]

(common_py3) PS E:\virtual_env_all\common_py3\Scripts> python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> input_list = [int(x.strip()) for x in input("enter list:").strip()[1:-1].split(",")]
enter list:[2,4,5]
>>> input_list
[2, 4, 5]
>>> type(input_list)
<class 'list'>
>>>



回答14:


Try this:

numbers = raw_input()
numberlist = list(numbers)



回答15:


k = []
i = int(raw_input('enter the number of values in the list '))
l = 0
while l < i:
    p = raw_input('enter the string ')
    k.append(p)
    l= l+1


print "list is ", k



回答16:


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



来源:https://stackoverflow.com/questions/4663306/get-a-list-of-numbers-as-input-from-the-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!