-3

I'm new. I would like to create few arrays from two arrays. Input arrays have the same number of rows and columns:

a = [1, 2, 3]
b = [4, 5, 6]

I would like to get:

test1 = [1, 4] 
test2 = [2, 5] 
test3 = [3, 6]

I tried to write some code as below:

test = [] 
a = [1, 2, 3] 
b = [4, 5, 6] 
for i in range(len(a)): 
    test[i] = [a[i], b[i]]

But it doesn't work. How can I get the desired output? I see that you don't understand me. I try to correct: - In reality I have much more variable than three that why I can't write:

test1, test2, test3 = zip(a,b)

- I really need this in format:

test1=[1,4]
   .
   .
   .
testi=[ , ]
  • I need separate arrays because I would like to use this for doing chart.
  • Some of you think that I don't need separate arrays, ok, but how can I create chart with 67 curves?:

chart is XY axis, for example: data X = [1,10], data for Y: Y1 = test1 = [1,4] . . Y67 = test67 = [70,90] so how I get data for Y axis for 67 curves if i have list of lists?

Monika
  • 3
  • 3
  • 5
    use `zip()` function – Mohammad Yusuf Jan 17 '17 at 12:42
  • 1
    Possible duplicate of [How can I iterate through two lists in parallel in Python?](http://stackoverflow.com/questions/1663807/how-can-i-iterate-through-two-lists-in-parallel-in-python) – Mohammad Yusuf Jan 17 '17 at 12:43
  • (test1, test2, test3) = zip(a, b) – dpa Jan 17 '17 at 12:44
  • I tried use `zip()` but I got: [[1,4],[2,5],[3,6]] and I don't know how to separate – Monika Jan 17 '17 at 12:45
  • Are you using Python 2 or Python 3? Python 3 made a small but important change in the way that `zip` behaves. – PM 2Ring Jan 17 '17 at 12:48
  • @dpa FWIW, you don't need the parentheses on the left hand side; the commas create a tuple, the parentheses are only needed in certain situations (mostly to prevent ambiguity). – PM 2Ring Jan 17 '17 at 12:50
  • 1
    @Monika Why do you _need_ to separate them? A list of lists is usually _much_ more useful than having 3 separate lists. And when you have a bunch of variables with numeric names like `test1`, `test2`, `test3`, it's a strong sign that you _should_ be using a list instead of separate variables. – PM 2Ring Jan 17 '17 at 12:52
  • I need separate because my next step is chart with three different curves – Monika Jan 17 '17 at 12:58
  • @Monika `test1, test2, test3 = zip(a, b)` – Tagc Jan 17 '17 at 12:59
  • In reality I have much more variable than three, so can't write: `test1, test2, test3 = zip(a, b)` – Monika Jan 17 '17 at 13:02
  • 1
    @Monika You're saying contradictory things. If you're going to have many more than three variables (and if the number is likely to differ in different contexts), why not just store them as a list of lists? – Tagc Jan 17 '17 at 13:04
  • 1
    I'm pretty sure this is an XY problem. – Tagc Jan 17 '17 at 13:05
  • I would like to use this arrays for chart – Monika Jan 17 '17 at 13:08
  • What library are you using to plot the curves? Is it matplotlib? – PM 2Ring Jan 17 '17 at 14:01

4 Answers4

0

You can use the bult-in zip() method.

list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]

zip(list_a, list_b)

Output:

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

If you want lists, instead of tuples, you can use a list comprehension like this one:

lists = [list(i) for i in zip(list_a, list_b)]

Output:

[[1, 5], [2, 6], [3, 7], [4, 8]]
dot.Py
  • 5,007
  • 5
  • 31
  • 52
0

You could use append to add new element to your list if you want to proceed your way :

test=[] 
a = [1,2,3] 
b = [4,5,6] 
for i in range(len(a)): 
    test.append([a[i], b[i]])

print test
>>> [[1,4], [2,5],[3,6]]

or you could use zip() :

print zip(a, b)
>>> [(1,4), (2,5), (3,6)]
iFlo
  • 1,442
  • 10
  • 19
  • I don't want output looks like this: `[(1,4), (2,5), (3,6)]`. I would like got three different arrays – Monika Jan 17 '17 at 12:51
  • 1
    You three different array are : `test[0]`, `test[1]` and `test[2]` – iFlo Jan 17 '17 at 12:55
  • But this is only example. In reality I have much more variable than test1, test2 and test 3. That why I used for loop – Monika Jan 17 '17 at 13:00
  • Then you will have a big array containing your smaller arrays, this is the most convenient way to manage your issue. – iFlo Jan 17 '17 at 13:02
  • But I really need few arrays like: test1, test2, ... test67. Beacuse I would like to use this arrays for chart – Monika Jan 17 '17 at 13:05
  • your few arrays are in array `test`.. Why do you don't understand ? – iFlo Jan 17 '17 at 13:06
  • 1
    @Monika You _really_ **don't** need 67 separate lists. That will make your code into a horrible mess! The sane way to do this is with a list of lists (or a list of tuples), as we keep trying to tell you. – PM 2Ring Jan 17 '17 at 13:26
  • OK, so suppose you find some way to create a bunch of arrays bound to different different names. How then would you deal with them? The point that these commenters are trying to make is that dynamically-created variables are almost always a terrible idea, because you can't write code in advance to process them. If you have your arrays in a some other structure (a list or a dictionary) then you can iterate over them or pull individual arrays out to work on them individually. – holdenweb Jan 17 '17 at 13:47
0

Since you wanted to have the outputs as three explicit arrays, you can use this:

a = [1, 2, 3]
b = [4, 5, 6]
test1, test2, test3 = zip(a, b)
Tagc
  • 8,736
  • 7
  • 61
  • 114
0

In case if you need separate variables to use in function

vars = zip(a, b)
chart_function(*vars) # if chart_function accepts variables like v1, v2, ...
dpa
  • 427
  • 4
  • 16