Laex
Laex

Reputation: 95

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.

Upvotes: 1

Views: 5497

Answers (5)

Fatema Khanom
Fatema Khanom

Reputation: 1

b = 10 While b >= 0

  b = b - 2

  Print (b)

Upvotes: -1

Saumya Mehta
Saumya Mehta

Reputation: 191

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

Upvotes: 1

nsky80
nsky80

Reputation: 115

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]

Upvotes: 2

hygull
hygull

Reputation: 8740

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

Upvotes: 1

Rolando cq
Rolando cq

Reputation: 638

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]

Upvotes: 0

Related Questions