1

I created a list to represent a 2-dim matrix:

mylist = []
while (some condition):
    x1 = ...
    x2 = ...
    mylist.append([x1,x2])

I would like to test if each entry in the second column of the matrix is bigger than 0.45, but I meet some difficulty:

>>> mylist
[[1, 2], [1, -3], [-1, -2], [-1, 2], [0, 0], [0, 1], [0, -1]]
>>> mylist[][1] > 0.4
  File "<stdin>", line 1
    mylist[][1] > 0.4
           ^
SyntaxError: invalid syntax
>>> mylist[:,1] > 0.4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not tuple

Given that mylist is a list of sublists, how can I specify all the second components of all its sublists?

Is it good to choose list to represent the 2-dim matrix? I chose it, only because the size of the matrix is dynamically determined. What would you recommend?

Thanks!

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Tim
  • 1
  • 141
  • 372
  • 590
  • You might want to consider [`numpy`](http://www.numpy.org/) when working this this kind of data. You can work with vectorised functions rather than loops which can bring *significant* speed increases. – Ffisegydd Apr 27 '14 at 18:05
  • Why did you try `mylist[][1]` ?? – Grijesh Chauhan Apr 27 '14 at 18:07
  • because, ... I don't know in which language I was using it. Dear @GrijeshChauhan – Tim Apr 27 '14 at 18:10

2 Answers2

3

Use all() like this:

>>> lst = [[1, 2], [1, -3], [-1, -2], [-1, 2], [0, 0], [0, 1], [0, -1]]
>>> all(x > 0.45 for _, x in lst)
False

If you need a list of booleans then use a list comprehension:

>>> [x > 0.45 for _, x in lst]
[True, False, False, True, False, True, False]

mylist[][1] is an invalid syntax, but if you can use NumPy then you can do something like:

In [1]: arr = np.array([[1, 2], [1, -3], [-1, -2], [-1, 2], [0, 0], [0, 1], [0, -1]])

In [2]: all(arr[:,1] > 0.45)
Out[2]: False

In [4]: arr[:,1] > .45
Out[4]: array([ True, False, False,  True, False,  True, False], dtype=bool)
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • thanks, I need to have a result as `[True, False, False, True, False, True, False]`, each for each component. – Tim Apr 27 '14 at 18:07
  • @Tim Replace `all()` with a list comprehension, see the updated answer. – Ashwini Chaudhary Apr 27 '14 at 18:09
  • "in *pure* Python" -- Is `mylist[][1]` valid anywhere else? – Grijesh Chauhan Apr 27 '14 at 18:09
  • What does `_` mean in list comprehension? – Tim Apr 27 '14 at 18:12
  • 1
    @Tim It's just a [throwaway variable](http://stackoverflow.com/questions/5893163/underscore-in-python) – Ashwini Chaudhary Apr 27 '14 at 18:13
  • Thanks! A slighly changed question. Is there a quick way to return `[1, -1, -1, 1, -1, 1, -1]` where 1 indicates the component is greater than 0.4, -1 the component smaller than 0.4, 0 the component equals 0.4? – Tim Apr 27 '14 at 18:16
  • @Tim You can use a [conditional expression](https://docs.python.org/2/reference/expressions.html#conditional-expressions) inside the LC. Related: [if else in a list comprehension](http://stackoverflow.com/questions/4406389/if-else-in-a-list-comprehension) – Ashwini Chaudhary Apr 27 '14 at 18:17
  • @Tim `[1 if x > n else 0 if x == n else -1 for _, x in lst]` should do it, just replace `n` with 0.45. – Ashwini Chaudhary Apr 27 '14 at 18:21
  • @GrijeshChauhan Not that I know of, I've re-phrased my sentence. :-) – Ashwini Chaudhary Apr 27 '14 at 18:23
  • Aशwiniचhaudhary Actually *not exactly* but `int mylist[][1] = {1, 2, 3};` [is valid in C](http://codepad.org/OS0yOcOG), that is the reason I asked @Tim as I had impression he is confuse `row` value can only be omitted in declaration. He was using it in C, C++ or Java – Grijesh Chauhan Apr 27 '14 at 18:31
2

@Aशwini चhaudhary's solution is fantastic if you continue to use lists.

I would suggest you use numpy though as it can provide significant speed increases through vectorised functions, especially when working with larger datasets.

import numpy as np

mylist = [[1, 2], [1, -3], [-1, -2], [-1, 2], [0, 0], [0, 1], [0, -1]]

myarray = np.array(mylist)

# Look at all "rows" (chosen by :) and the 2nd "column" (given by 1).
print(myarray[:,1]>0.45)
# [ True False False  True False  True False]
Ffisegydd
  • 51,807
  • 15
  • 147
  • 125