I have a number n and I need to get from a user n variables in one line.
As far as i know, it's pretty easy to do if you know exactly how many variables you have.
*variables* = map(int, input().split())
But if I don't know how many variables there are, what should I do?
Also, I'm asked to put the result in array.
Unfortunately, I've just started learning Python, so I have absolutely no idea how to do it and can't show any code I've tried.
User input being taken as a space separated string:
1 2 3 4 5
This being the code you are dealing with:
map(int, input().split())
Stating you need a list, then just store it in a single variable:
inputs = map(int, input().split())
However, dealing with Python 3, you will end up with map object. So if you actually need a list type, then just call list on the map function:
inputs = list(map(int, input().split()))
Demo:
>>> inputs = list(map(int, input().split()))
1 2 3 4 5
>>> type(inputs)
<class 'list'>
>>> inputs
[1, 2, 3, 4, 5]
Related
I've created a program to generate 5 random integers from 1-10 and add them to an empty set. For some reason, when I run the program, it will sometimes return 4 integers, and other times 5. What is happening here?
import random
set1 = set()
for x in range(5):
integer = random.randint(1,10)
set1.add(integer)
print(set1)
You're using a set, sets can't contain duplicates, if the same number is generated twice it will only occur in the set once, theoretically its possible your output would only have 1 number in it (if the same number was added 5 times).
You should use a list instead:
import random
output = []
for x in range(5):
integer = random.randint(1,10)
output += [integer]
print(output)
The easiest way to generate n random unique numbers is to use random.sample:
>>> import random
>>> set(random.sample(range(1, 11), 5))
set([8, 9, 5, 6, 10])
Note that you should use range(1, 11) if you also want to include the number 10.
Python sets will not show duplication. A simple way to fix your script is to use a list instead of a set. One thing to note however, is if you are going to want to use all the numbers together like 12345, this won't do the trick. The following script will return a list as [1, 2, 3, 4, 5].
list1 = [] # Make an empty list
for x in range(5):
# randomly choose a number from 1 - 10 and append it to our list 5 times
integer = random.randint(1,10)
list1.append(integer)
print(list1) # print the list
This is my code:
a,b=input().split()
for x in range(a,b):
print(list(x))
Why is this not working?
It says can't interpret str as an int.
And when I put it under int(), it says int() argument must be a string.
I want this:
Input:
2 6
Output:
3
4
5
6
This does exactly what you expect:
a,b=input().split()
for x in range(int(a)+1,int(b)+1):
print(x)
input() returns a string, and split() returns a list of strings. You have to convert them to integers before feeding them to range. Also, in print(x) x is already a number and there is no reason to convert it into a list (and it would not work, either).
Simply replace print(list(x)) with print(x) . I don't see any reason to use list() . You can also use range(a,b+1) instead of range(a,b) if you want the last number printed to be the same as b.
By default, input() accepts values as string and split() returns a list of strings. You need to convert a and b to int after input. Also, you need to get rid of list(x) and just print(x) since int in Python is not iterable.
The correct code is:
a, b = map(int, input().split())
for x in range(a + 1, b + 1):
print(x)
OR
a, b = [int(i) for i in input().split()]
for x in range(a + 1, b + 1):
print(x)
In both cases, for input 2 6, the output is
3
4
5
6
Total newb to Python here. I'm working on CodeAbbey's problems using Python 3, and I'd like help to make the code for user input shorter.
Let's say I want to get this input from the user:
3
2 3
4 5
6 7
First line is number of cases, and each of the following lines are the cases themselves with 2 parameters. I've figured out to do it in this way so far:
N=int(input('How many cases will you calculate?\n'))
print('Input parameters separated by spaces:')
entr = [list(int(x) for x in input().split()) for i in range(N)]
The thing is I'd rather to ask all the input in the list comprehension, and then assign N=entr[0]. But how do I get the list comprehension to break the input into lines without using range(N)?
I tried:
entr = [list(int(x) for x in input().split()) for x in input()]
but it doesn't work.
I don't see the benefit of doing this in a list comprehension, but here is a solution that allows all data to be copy-pasted in:
entr = [list(int(x) for x in input().split())
for i in range(int(input()))]
N = len(entr)
Your solution was pretty close. The outer iteration just needed to be given something to iterate on (using range()) rather than a single number.
Yeah you can try this in the list comprehension
cases = [input().split() for _ in range(int(input()))]
flatList = [int(item) for elem in cases for item in elem]
print(flatList)
I simply used this for python list input
n = int(input())
a = [list(map(input().split())) for i in range(n)]
print(a)
'''
3
2 3
4 5
6 7
[[2, 3],[4, 5], [6,7]]
'''
arr = []
for i in range(int(input())):
a = list(map(int,input().split()))
arr.append(a)
print(arr)
'''
3
2 3
4 5
6 7
[[2, 3],[4, 5], [6,7]]'''
''' or in single list '''
arr = []
for i in range(int(input())):
a,b = map(int,input().split())
arr += [a,b]
print(arr)
'''
3
2 3
4 5
6 7
[2,3,4,5,6,7]'''
Im newbie, how can i use this editor for python output console?
Here input is taken as int . But you can change it acc. To your need of input.
And 4 is the no. Of input() you will insert that can also edit acc. To your need
'''enter = list(i for i in (int(input() for i in range(4)))
print(enter)'''
I am trying to read array elements as
4 #no. of elements to be read in array
1 2 3 4
what i have tried by referring other answers
def main():
n=int(input("how many number you want to enter:"))
l=[]
for i in range(n):
l.append(int(input()))
this works fine if i give input as
4 #no. of elements to be read
1
2
3
4
but if i try to give like
4 #no. of element to be read
1 2 3 4
I get error as:
ValueError: invalid literal for int() with base 10: '1 2 3 4'
Please help me with this
Since there's no input delimiters in Python, you should use split and split the input that you've received from the user:
lst = your_input.split()
Your first approach is OK, for the second way use this:
n=int(input("how many number you want to enter:"))
l=map(int, input().split())[:n] # l is now a list of at most n integers
This will map the function int on the split parts (what split gives) of the user input (which are 1, 2, 3 and 4 in your example.
It also uses slicing (the [:n] after map) to slice in case the user put more integers in.
The input() function returns a string that the user enters. The int() function expects to convert a number as a string to the corresponding number value. So int('3') will return 3. But when you type in a string like 1 2 3 4 the function int() does not know how to convert that.
You can either follow your first example:
n = int(input('How many do you want to read?'))
alist = []
for i in range(n):
x = int(input('-->'))
alist.append(x)
The above requires you to only enter a number at a time.
Another way of doing it is just to split the string.
x = input('Enter a bunch of numbers separated by a space:')
alist = [int(i) for i in x.split()]
The split() method returns a list of numbers as strings excluding the spaces
n = input("how many number you want to enter :")
l=readerinput.split(" ")
i have a python code which should read in 2 integers from standard input till user presses Ctrl+D (i.e EOF)
and do some processing .I tried the following code :
n,k=map(int,[a for a in sys.stdin.read().split()])
Here , when i enter two integers the programme accepts it and when i press Ctrl+D it shows the correct output , like:
6 3
but when i put in 2 pairs in intergers like:
6 3
12 2 and then press Ctrl+D then instead of the desired result i get error that:
[i]ValueError: Too many values to upack[/i]
So how do i correct the code for it to work properly?
I intend to have the shortest possible code for that
Thanks.
>>> x=map(int,[a for a in sys.stdin.read().split()])
2 3 4 5
>>> x
[2, 3, 4, 5]
and work against the list; this so you will accept a variable number of ints if required to do so
The problem is not in how you read from stdin. Entering 6 3 essentially makes your code equivalent to
n, k = [6, 3]
which will work fine. Entering 6 3 12 2 though will result in
n, k = [6, 3, 12, 2]
which does not work, since you try to unpack a sequence of four values to only two targets. If you want to ignore everything beyond the first two numbers, try
n, k = [int(a) for a in sys.stdin.read().split()][:2]
If you want to iterate through the numbers read from stdin in pairs, you can use
numbers = (int(a) for a in sys.stdin.read().split())
for n, k in zip(numbers, numbers):
# whatever