views:

310

answers:

6

Hi, I'm a Matlab user needing to use Python for some things, I would really appreciate it if someone can help me out with Python syntax:

(1) Is it true that lists can be indexed by tuples in Python? If so, how do I do this? For example, I would like to use that to represent a matrix of data.

(2) Assuming I can use a list indexed by tuples, say, data[(row,col)], how do I remove an entire column? I know in Matlab, I can do something like

 new_data = [data(:,1:x-1) data(:,x+1:end)];

if I wanted to remove column x from data.

(3) How can I easily count the number of non-negative elements in each row. For example, in Matlab, I can do something like this:

 sum(data>=0,1)

this would give me a column vector that represents the number of non-negative entries in each row.

Thanks a lot!

+7  A: 

You should look into numpy, it's made for just this sort of thing.

Ned Batchelder
numpy will do everything asked in the question, with nearly exactly the same syntax as matlab.
Autoplectic
Numpy arrays are the way to go. I recommend this page to help make the transition easier - http://www.scipy.org/NumPy_for_Matlab_Users
Raja
+3  A: 
  1. No, but dicts can.
  2. Sounds like you want a "2d array", matrix type, or something else. Have you looked at numpy yet?
  3. Depends on what you choose from #2, but Python does have sum and other functions that work directly on iterables. Look at gen-exprs (generator expressions) and list comprehensions. For example:
row_count_of_non_neg = sum(1 for n in row if n >= 0)
# or:
row_count_of_non_neg = sum(n >= 0 for n in row)
# "abusing" True == 1 and False == 0
Roger Pate
A: 

(1) I don't think you can use a tuple as an index of python list. You may use list of list ( e.g. a[i][j]) but it seems that it's not your point. You may use a dictionary whose key is tuple.

d = { (1,1):1, (2,1):2  ... } 

(2) If you don't mind about the performance,

map( lambda x: d.remove(x) if x[1] = col_number, d.keys() )

(3) You can also use the filter to do that.

sum(
map( lambda x:x[1], filter(lambda x,y: x[1] == row_num and y > 0, d.items()))
)
kjshim
Combining map or filter with lambdas is awkward, compare to a generator expression: `sum(y for x, y in d.items() if x[1] == row_num and y > 0)` (which should be equivalent to your code, but your code appears to have a bug).
Roger Pate
A: 

No, it isn't the case that a list can be indexed by anything but an integer. A dictionary, however, is another case. A dictionary is a hash table consisting a key-value pairs. Keys must be unique and immutable. The value can be objects of any type, including integers, tuples, lists, or other dictionaries. For your example, tuples can serve as keys, since they are immutable. Lists, on the other hand, aren't and, thus, can't be dictionary keys.

Some of the capabilities you've asked about could be implemented as a combination of a dictionary and list comprehensions. Others would require subclassing the dictionary and adding methods to implement your desired functionality.

sizzzzlerz
+2  A: 

Here's an example of how to easily create an array (matrix) in numpy:

>>> import numpy
>>> a = numpy.array([[1,2,3],[4,5,6],[7,8,9]])

here is how it is displayed

>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

and how to get a row or column:

>>> a[0,:]
array([1, 2, 3])
>>> a[:,0]
array([1, 4, 7])

Hope the syntax is clear from the example! Numpy is rather powerfull.

Morlock
+2  A: 

I agree with everyone. Use Numpy/Scipy. But here are specific answers to your questions.

  1. Yes. And the index can either be a built-in list or a Numpy array. Suppose x = scipy.array([10, 11, 12, 13]) and y = scipy.array([0, 2]). Then x[[0, 2]] and x[y] both return the same thing.

  2. new_data = scipy.delete(data, x, axis=0)

  3. (data>=0).sum(axis=1)

Careful: Example 2 illustrates a common pitfall with Numpy/Scipy. As shown in Example 3, the axis property is usually set to 0 to operate along the first dimension of an array, 1 to operate along the second dimension, and so on. But some commands like delete actually reverse the order of dimensions as shown in Example 2. You know, row major vs. column major.

Steve