0

I was attempting to do one kata problem in Python, where I have two lists of input, let's say weight and value, which was getting input in the order (value1, weight1, value2, weight2,....) If it were C++, I could just take one element at a time with cin to my arrays. But with Python I don't know how to take this input. If the input is like

60 10 100 20 120 30 (in a single line)

I want my two lists val and w have values

val=[60,100,120]
w=[10,20,30]

How to take this kind of inputs in Python? Is it possible to read only one input at a time, like cin does in C++?

Pratik
  • 1,351
  • 1
  • 20
  • 37
  • Does this answer your question? [Get a list of numbers as input from the user](https://stackoverflow.com/questions/4663306/get-a-list-of-numbers-as-input-from-the-user). Your title is really misleading. You ask about the complete opposite... – Tomerikoo May 23 '20 at 10:00
  • @Tomerikoo No, I knew how to take whole list as an input, my question was if we can get single element like cin in C++ does? or in some way where we can directly assign lists to the input without taking extra list – Pratik May 23 '20 at 10:03

5 Answers5

4

You can read space-separated input to a list using splitand then use slicing to get the odd/even indexed elements:

val = input().split()
val = [int(i) for i in val] #to integer
w = val[1::2] # get only odd indices
val = val[0::2] # get only even indices

print(val) # [60,100,120]
print(w) # [10,20,30]

You can then use regular indexing to get individual elements in the lists.

ccl
  • 2,378
  • 2
  • 12
  • 26
  • right, this will require one extra list to take input and then we can split it to 2 other arrays based on indexing. This will surely work, just wondering if we have a better way – Pratik May 23 '20 at 10:01
  • If you have 2 rows of input, you could do `val = input().split()` `w = input().split()`, `val, w = [int(i) for i in val], [int(i) for i in w]`. – ccl May 23 '20 at 10:03
  • Read the question. The values are interwined in one line – Tomerikoo May 23 '20 at 10:03
  • Updated, this should be the simplest method (using slicing) – ccl May 23 '20 at 10:08
  • 1
    This is a good way. You might just put the int conversion and input in one line: `val = [int(i) for i in input().split()]` – Tomerikoo May 23 '20 at 10:10
2

This should do it.

values = input("Enter values: ").split(" ")

if(len(values) % 2 != 0):
    print("Error, uneven number of values and weights")
else:
    val = []
    w = []
    for i in range(0, len(values) - 1, 2):
        val.append(values[i])
        w.append(values[i+1])

    print("val: ", val)
    print("w: ", w)
A. Baena
  • 121
  • 5
1

Here's a simple solution:

a = "60 10 100 20 120 30"
split_a = a.split(" ")
val = [e for (i, e) in enumerate(split_a) if i % 2 == 0]
w = [e for (i, e) in enumerate(split_a) if i % 2 == 1]
# or shorter with slicing (start:stop:step)
val = [e for e in split_a[0::2]]
w = [e for e in split_a[1::2]]
m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
1

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]))
Amal K
  • 4,359
  • 2
  • 22
  • 44
0

I am not sure what cin does but I assume the input you have is a string. One way you could do is to split it into a list of ints. And then create your val and w lists and append your values into the lists. (The first, third, fifth, etc would be your val list)

my_input_list = my_input.split(' ')

Read more here.

JohnDoe_Scientist
  • 590
  • 1
  • 6
  • 18