4

I had thought that if you ran perhaps print mdarray[::][1], you would print the first sub-element of every element in the array. Where did I go wrong with this?

I especially need this for a p.plot(x,y[::][1]) where I definitely do not want to use a for loop, as it is horribly slow, unless I'm getting things confused.

What am I getting wrong? Thanks!

EDIT

I still don't know where I got the [::] thing but I solved my problem with either

p.plot(x,c[:,1],color='g',label="Closing value")

or

p.plot(x,[i[1] for i in c],color='g',label="Closing value")

There doesn't seem to be any appreciable difference in time, so I guess I'll use the second because it looks more pythonic/readable to me. Or am I missing something?

Thanks for all of the help!

Peter Ohaver
  • 87
  • 1
  • 10

4 Answers4

5

If mdarray is a numpy array you can access first column of it with mdarray[:,0]

In [8]: mdarray = np.array([[1, 2, 4], [4, 5, 6], [7, 8, 9]])

In [9]: mdarray
Out[9]:
array([[1, 2, 4],
       [4, 5, 6],
       [7, 8, 9]])

In [10]: mdarray[:,0]
Out[10]: array([1, 4, 7])

UPD

Quick and dirty test

In [28]: mdarray = np.zeros((10000,10000))

In [29]: %timeit -n1000 [x[0] for x in mdarray]
1000 loops, best of 3: 2.7 ms per loop

In [30]: %timeit -n1000 mdarray[:,0]
1000 loops, best of 3: 567 ns per loop
Konstantin
  • 24,271
  • 5
  • 48
  • 65
  • Thanks, this got my code working, but it switching from a list to an array somehow messed up my xticks so I may need to either find some other way or figure out another fix for my ticks. – Peter Ohaver Apr 29 '15 at 04:41
  • what do you mean? matplotlib works with numpy arrays just fine. You might want to ask another questions regarding `plot` – Konstantin Apr 29 '15 at 04:45
  • Actually, I figured out what it was that was causing the problem. I guess I made another change since I'd last successfully run my code. Thanks for your help! – Peter Ohaver Apr 29 '15 at 04:53
  • @PeterOhaver I've aded quick test to my answer. Take a look – Konstantin Apr 29 '15 at 06:51
  • Interesting, thank you very much! It seems like the "more readable" one is slower, which makes sense in this case. Thanks! – Peter Ohaver Apr 29 '15 at 07:00
2

What you did:

You used mdarray[::]. That makes a (shallow) copy of mdarray. Then you accessed the second element of it with [1]. [0] would be the first.

What you can do is a list comprehension:

[item[0] for item in mdarray]

This will return a list of the first elements of the lists in mdarray.

Talking about loops: A (one time) loop is rather effective to access something. Internally all the magic functions (like the comprehension above) are iterating over the data.

Klaus D.
  • 13,874
  • 5
  • 41
  • 48
  • Yeah, I use both the first and second elements so I just messed up 0 vs. 1 in between the title and body. The problem with using a list comprehension is that when I plot everything with a label, it then creates ~40,000 entries in the legend. – Peter Ohaver Apr 29 '15 at 04:30
  • So you actually have two problems? Getting the right items and plotting the legend properly. – Klaus D. Apr 29 '15 at 04:32
  • The legend problem only pops up when I use list comprehension. I think I just figured it out, though, using `c=p.asarray(c)` then `p.plot(x,c[:,1],label="c")`. Thanks! **EDIT:** Actually, my fix messes up my tick marks, so I might still need to fix the legend problem with list comprehension. – Peter Ohaver Apr 29 '15 at 04:38
  • Actually, that may have been a one-time thing with the thousands of legend entries. Using the list comprehension does seem to work best. Thanks! – Peter Ohaver Apr 29 '15 at 04:55
0

How about:

>>> Matrix = [[x for x in range(5)] for x in range(5)]
>>> Matrix
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
>>> [item[0] for item in Matrix]
[0, 0, 0, 0, 0]

As for ::, you can read more about it here, It will return the same list.

Community
  • 1
  • 1
Ofiris
  • 6,047
  • 6
  • 35
  • 58
  • Thanks, I already saw that page but it doesn't really address just [::], but rather [::n], which I already understand and actually have just a few lines down in my actual code. – Peter Ohaver Apr 29 '15 at 04:32
-1

Not sure whether you use array or list, but for Python's lists:

Python 2:

>>> mdarray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> zip(*mdarray)[0]
(1, 4, 7)

Python 3:

>>> mdarray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> list(zip(*mdarray))[0]
(1, 4, 7)

Or for the special case of index 0:

>>> mdarray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> next(zip(*mdarray))
(1, 4, 7)
Stefan Pochmann
  • 27,593
  • 8
  • 44
  • 107