No you cannot as far as I know. input()
is a function that reads one line at a time as an entire string.
A popular way to make a list out of an entire line of space separated integers is using the in-built map()
function and split()
method of a string object:
numbers = map(int, input.split())
numbers
will now contain the list of numbers on that line.
Firstly, input()
reads the entire line as a string including spaces.
split()
function splits the input at space, you can also pass in a string argument to make it split anywhere you want, i.e a custom delimiter like ,
and creates a list of strings of those numbers
map()
calls int()
on each of the numbers in that list.
In C++, just like you need to know the size of input in advance, you can run a for loop on the list to now split values and weights into two lists.
val, w = [], []
for i in range(len(numbers)):
if i % 2 == 0: val.append(numbers[i])
else: w.append(numbers[i])
For better performance(relevant for large input size), you can even skip the map()
step, and do something like this:
numbers = input().split()
val, w = [], []
for i in range(len(numbers)):
if i % 2 == 0: val.append(int(numbers[i]))
else: w.append(int(numbers[i]))