简体   繁体   中英

Taking n number of integer inputs from a single line in python

So basically I take an n value with n = int(input("number of boats")) from user, then taking n amount of integer inputs from a single line (let's say my input is 2 6 3 6 4 7 4 and they wrote 2 , I'd only take the first two numbers 2 6 ) and appending that into a list (which I define before as mylist = [] ). I want to have them as integers and not a string in my list. How can I do this?


EDIT:

Okay perhaps my wording wasn't the best, so I'll try explaining differently. I'm taking an input from a .txt file and the file has:

3 23 56 36 48 46 97

The 3 at the start determines how many boats there are, and the 23 56 for example are values for the first boat. I want to take input that determines how many boats, then take all the values as input and put them all into one list [23, 56, 36, 48, 46, 97] . Note that I have to use input and not file reading because there will be different values tested. I need the values as integers so I can't take every line as a string.

You can try like this.

Note: I think, there is no need to take explicit value for n .

>>> import re
>>> 
>>> s = '12 34      56     34   45'
>>> l = re.split(r'\s+', s)
>>> l
['12', '34', '56', '34', '45']
>>> 
>>> [int(n) for n in l]
[12, 34, 56, 34, 45]
>>> 
>>> # Using the above concept, it can be easily done without the need of explicit n (that you are taking)
... 
>>> mylist = [int(n) for n in re.split('\s+', input("Enter integers (1 by 1): ").strip())]
Enter integers (1 by 1): 12 34   56 67  99 34 4 1 0 4 1729
>>> 
>>> mylist
[12, 34, 56, 67, 99, 34, 4, 1, 0, 4, 1729]
>>> 
>>> sum(mylist)
2040
>>> 

You should try this code:

n = int(input("number of boats:"))
mylist = []
for _ in range(n):            # Taking n lines as input and add into mylist
    mylist.extend(list(map(int, input().rstrip().split())))
print("mylist is:", mylist)

Output as:

number of boats:3
23 56
36 48
46 97
mylist is: [23, 56, 36, 48, 46, 97]

One method you can try :

numlist = []
n = stdin.readline()
for _ in range(int(n)):
    numlist.extend(stdin.readline().split())
stdout.write(str(numlist)) 

Output for this method :

2 
1 2
3 4 5

The time taken by this method:

import timeit
setup = "from sys import stdin,stdout"
statement = '''
numlist = []
n = stdin.readline()
for _ in range(int(n)):
    numlist.extend(stdin.readline().split())
stdout.write(str(numlist))   
'''
print (timeit.timeit(setup = setup, 
                    stmt = statement, 
                    number = 1) )

Output with time taken to execute:

2 
1 2
3 4 5
['1', '2', '3', '4', '5']7.890089666

If the idea is to take n inputs you can do this:

mylist = list()
n = int(input("number of boats"))
for i in range(n):
    mylist.append(int(input("integer number")))

If the idea is having a string with values and letting the user to decide how may numbers of that string to have youmay do this:

boats = '2 6 3 6 4 7 4'
n = int(input("number of boats"))
boats = list(map(int, boats.split(' ')))
mylist = boats[:n]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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