T
T
Timebird2016-03-03 02:34:41
Python
Timebird, 2016-03-03 02:34:41

How to make return return multiple arrays?

Hello.
The question is this: I am multiplying the first, second, third, ..., j-th values ​​of one array by each of the elements of another array. As a result, when printing, j arrays are obtained. But I can’t figure out how exactly to stuff j arrays into return? When called in a subsequent function, all operations occur only with the first of them.

def getcoordinates(array1, array2):
  """Перемножаем [cначала первое, потом второе, ..., j-ое] значения первого массива на каждый из элементов второго массива."""

    for j in array1:
        array3 = [j * i for i in array2]
        print(array3)
        return array3

def buildgraph(array3, array1):
  print(array3) #тут выводится только первый массив вместо j-ого количества массивов

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Andrey Andryushchenko, 2016-03-03
@Timebird

return terminates the execution of the function.
For return to return j arrays, you must then return a two-dimensional array. To do this, we declare an array before for; arrayRes=[], and in the loop, instead of return, we add each array3 to our array: arrayRes.append(array3), and at the end of the function, after the loop, we writereturn arrayRes

V
Vladimir Olohtonov, 2016-03-03
@sgjurano

You can also return generators, like this:
def func():
for i in xrange(10):
yield xrange(10)

M
Mogidin, 2016-03-03
@Mogidin

def getcoordinates(array1, array2):
  for j in array1:
    array3 = [j * i for i in array2]
    yield array3

array1 = [1,2,3,4,5,6,7,8,9]
array2 = [10,20,30,40,50,60,70,80,90]

for a in getcoordinates(array1, array2):
  print a

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question