0

I start with two numpy arrays, the "x values" and the "y values":

import numpy as np
x = np.arange(100)
y = np.arange(100)

The output is

[ 0  1  2  3  4 ..... 96 97 98 99]
[ 0  1  2  3  4 ..... 96 97 98 99]

I would like to append these values together into an array of len() = 100 such that the output is

[ (0,0) (1,1) (2,2) (3,3) .... (98,98) (99,99) ]

How does one use indexing to both (A) put the pairs in the correct order and (B) put the paratheses ( and comma , in the correct order?

7
  • 3
    I'm fairly certain you can use zip(x,y) but I'm not 100% sure that will work properly with numpy Commented Jun 29, 2015 at 16:27
  • 2
    Why does your title mention strings, the question has nothing to do with strings. Commented Jun 29, 2015 at 16:34
  • If you want to still have a numpy array don't use zip Commented Jun 29, 2015 at 16:51
  • @PadraicCunningham Couldn't I just convert output list into a np.ndarray? Commented Jun 29, 2015 at 17:15
  • 1
    @ShanZhengYang, you can do it all in numpy, no point creating a list to turn it back into a numpy array Commented Jun 29, 2015 at 17:16

3 Answers 3

2

For your particular requirement, you can use the built-in zip function, which combines multiple lists at their corresponding indexes (that is ith index of all lists that are parameter to it in combined in the returned iterator).

Example -

import numpy as np
x = np.arange(100)
y = np.arange(100)
print(list(zip(x,y)))
>>> [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10), (11, 11), (12, 12), (13, 13), (14, 14), (15, 15), (16, 16), (17, 17), (18, 18), (19, 19), (20, 20), (21, 21), (22, 22), (23, 23), (24, 24), (25, 25), (26, 26), (27, 27), (28, 28), (29, 29), (30, 30), (31, 31), (32, 32), (33, 33), (34, 34), (35, 35), (36, 36), (37, 37), (38, 38), (39, 39), (40, 40), (41, 41), (42, 42), (43, 43), (44, 44), (45, 45), (46, 46), (47, 47), (48, 48), (49, 49), (50, 50), (51, 51), (52, 52), (53, 53), (54, 54), (55, 55), (56, 56), (57, 57), (58, 58), (59, 59), (60, 60), (61, 61), (62, 62), (63, 63), (64, 64), (65, 65), (66, 66), (67, 67), (68, 68), (69, 69), (70, 70), (71, 71), (72, 72), (73, 73), (74, 74), (75, 75), (76, 76), (77, 77), (78, 78), (79, 79), (80, 80), (81, 81), (82, 82), (83, 83), (84, 84), (85, 85), (86, 86), (87, 87), (88, 88), (89, 89), (90, 90), (91, 91), (92, 92), (93, 93), (94, 94), (95, 95), (96, 96), (97, 97), (98, 98), (99, 99)]

For Python 2.x , please note you do not need list(zip(...)) , since zip itself would return a list , but for Python 3.x , zip returns an iterator, and to print it we would need to convert it into a list.

Sign up to request clarification or add additional context in comments.

3 Comments

Is 'list' needed here? Should work without it as well or am I missing something?
@Cleb depends. zip returns a generator object so no, you don't need list() right away but eventually you will need to consume the generator to get the values out.
For python 3 , zip returns an iterator, if we just print it, we would get something like - <zip object at 0x00355D78> . For Python 2, it is not required I think
1

You can use np.dstack to get the columns :

>>> np.dstack((x,y))
array([[[ 0,  0],
        [ 1,  1],
        [ 2,  2],
        [ 3,  3],
        [ 4,  4],
        [ 5,  5],
        [ 6,  6],
        [ 7,  7],
        [ 8,  8],
        [ 9,  9],
           ...
        [99, 99]]])

And if you want to get tuple instead of list you can use map to convert it to tuple:

>>> map(tuple,np.dstack((x,y))[0])
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10), (11, 11), (12, 12), (13, 13), (14, 14), (15, 15), (16, 16), (17, 17), (18, 18), (19, 19), (20, 20), (21, 21), (22, 22), (23, 23), (24, 24), (25, 25), (26, 26), (27, 27), (28, 28), (29, 29), (30, 30), (31, 31), (32, 32), (33, 33), (34, 34), (35, 35), (36, 36), (37, 37), (38, 38), (39, 39), (40, 40), (41, 41), (42, 42), (43, 43), (44, 44), (45, 45), (46, 46), (47, 47), (48, 48), (49, 49), (50, 50), (51, 51), (52, 52), (53, 53), (54, 54), (55, 55), (56, 56), (57, 57), (58, 58), (59, 59), (60, 60), (61, 61), (62, 62), (63, 63), (64, 64), (65, 65), (66, 66), (67, 67), (68, 68), (69, 69), (70, 70), (71, 71), (72, 72), (73, 73), (74, 74), (75, 75), (76, 76), (77, 77), (78, 78), (79, 79), (80, 80), (81, 81), (82, 82), (83, 83), (84, 84), (85, 85), (86, 86), (87, 87), (88, 88), (89, 89), (90, 90), (91, 91), (92, 92), (93, 93), (94, 94), (95, 95), (96, 96), (97, 97), (98, 98), (99, 99)]
>>> 

Comments

0

You could use vstack

In [36]: xy = np.vstack((x,y)).T

In [37]: xy.shape
Out[37]: (100, 2)

In [38]: xy[0]
Out[38]: array([0, 0])

In [39]: xy[1]
Out[39]: array([1, 1])

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.