elvaqueroloconivel1
elvaqueroloconivel1

Reputation: 919

Store multiple values in arrays Python

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

Upvotes: 2

Views: 19282

Answers (5)

KhanJr
KhanJr

Reputation: 39

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

Upvotes: 0

jsfa11
jsfa11

Reputation: 513

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

Upvotes: 2

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

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)

Upvotes: 1

Juan Cerezo
Juan Cerezo

Reputation: 25

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)

Upvotes: 0

a_guest
a_guest

Reputation: 36249

You can use the following:

l = [int(x) for x in input().split(',')]
array_1 = l[::2]
array_2 = l[1::2]

Upvotes: 7

Related Questions