1

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.

5
  • 1
    You did it already with n. What's the problem with the rest of the numbers? Commented Feb 10, 2019 at 0:34
  • 2
    Post your code. Commented Feb 10, 2019 at 0:36
  • @mkrieger n determines how many of the integers I take from the input 2 6 3 6 4 7 4 Commented Feb 10, 2019 at 10:10
  • @PedroLobito unfortunately I am not allowed to share my code. Commented Feb 10, 2019 at 10:11
  • re.split(r'\s+', s) (where s is input string) and r'\s+' is regular expression) will allow you to split the input string based on whitespaces (tabs, spaces etc.). only simple spilt() is not good as user may enter any number of spaces/tabs while inputting integers on console. Please have a look at my answer. Commented Feb 10, 2019 at 10:37

5 Answers 5

2

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]
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

I like this solution a lot! Though I'm not allowed to import for this specific case :) I appreciate it though!
1

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

1 Comment

This solution seems very elegant, thanks a lot! @SatishKumarYadav has already provided a solution which I used, but yours is a good alternative!
0

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]

Comments

-1

b = 10 While b >= 0

  b = b - 2

  Print (b)

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.