2

I'm doing a code that needs to store the values of the inputs in two arrays. I'm gonna do a example.

INPUTS: 1,2,3,4,5,6,7,8

Array1= []
Array2= []

What I want to make is store the first value of the input in the array1 and the second in the array2. The final result will be this

Array1=[1,3,5,7]
Array2=[2,4,6,8]

Is possible to do that in python3? Thank you

I tried something like this but doesn't work

arr1,arr2 = list(map(int, input().split())) 
2
  • What have you tried so far, and what are you stuck on with your current attempts? Commented Nov 23, 2018 at 21:09
  • I tried something like this but doesn't work arr1,arr2 = list(map(int, input().split())) Commented Nov 23, 2018 at 21:10

5 Answers 5

7

You can use the following:

l = [int(x) for x in input().split(',')]
array_1 = l[::2]
array_2 = l[1::2]
Sign up to request clarification or add additional context in comments.

1 Comment

without list comprehension l = list(map(int, input().split(',')))
2

So, I assume that you can get the inputs into a list or 'array' using the split? It would be nice to somehow 'map' the values, and numpy probably would offer a good solution. Though, here is a straight forward work;

while INPUTS:
  ARRAY1.append(INPUTS.pop())
  if INPUTS:
    ARRAY2.append(INPUTS.pop())

2 Comments

if INPUTS length is odd, that will crash
It would have crashed. I edited. Just tried to keep it simple due to the example given.
1

your attempt:

arr1,arr2 = list(map(int, input().split())) 

is trying to unpack evenly a list of 8 elements in 2 elements. Python can unpack 2 elements into 1 or 2, or even use iterable unpacking like:

>>> arr1,*arr2 = [1,2,3,4]
>>> arr2
[2, 3, 4]

but as you see the result isn't what you want.

Instead of unpacking, use a list of lists, and a modulo to compute the proper destination, in a loop:

lst = list(range(1,9)) # or list(map(int, input().split()))  in interactive string mode

arrays = [[],[]]

for element in lst:
    arrays[element%2].append(element)

result:

[[2, 4, 6, 8], [1, 3, 5, 7]]

(change the order with arrays[1-element%2])

The general case would be to yield the index depending on a condition:

arrays[0 if some_condition(element) else 1].append(element)

or with 2 list variables:

(array1 if some_condition(element) else array2).append(element)

1 Comment

This only works with even / odd numbers alternating in lst. You could use arrays[len(arrays[0]) - len(arrays[1])].append(element) in order to decouple from element. Well, or simply enumerate(lst).
0

There is my solution in a Class ;)

class AlternateList:
    def __init__(self):
        self._aList = [[],[]]

        self._length = 0

    def getItem(self, index):
        listSelection = int(index) % 2

        if listSelection == 0:
            return self._aList[listSelection][int(index / 2)]
        else:
            return self._aList[listSelection][int((index -1) / 2)]

    def append(self, item):
        # select list (array) calculating the mod 2 of the actual length. 
        # This alternate between 0 and 1 depending if the length is even or odd
        self._aList[int(self._length % 2)].append(item)
        self._length += 1

    def toList(self):
        return self._aList

    # add more methods like pop, slice, etc

How to use:

inputs = ['lemon', 'apple', 'banana', 'orange']

aList  = AlternateList()

for i in inputs:
    aList.append(i)

print(aList.toList()[0]) # prints -> ['lemon', 'banana']
print(aList.toList()[1]) # prints -> ['apple', 'orange']

print(aList.getItem(3))  # prints -> "orange" (it follow append order)

Comments

0

The pythonic way

Here I have taken some assumptions according to above question:

  1. Given inputs only contain integers.

  2. odd and even are two arrays which contain odd numbers and even numbers respectively.

odd, even = list(), list()

[even.append(i) if i % 2 == 0 else odd.append(i) for i in list(map(int, input().split()))]

print("Even: {}".format(even))
print("Odd: {}".format(odd))

Comments

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.