Ask Your Question

Revision history [back]

One way you could do this is to use a conditional list comprehension; these take the form

[x for x in list if <condition>]

And since you will be iterating over a matrix, a double list comprehension is in order; these take the form

[x for b in a for x in b]

where a is the "outer list", and b is the "inner list". And since you want to know the indices and the entries together, we'll use the enumerate function. So here's a conditional double list comprehension which returns (i,j) such that deg(i,j) == 15:

sage: [(i,j) for (i,deg_i) in enumerate(deg) for (j,deg_ij) in enumerate(deg_i) if deg_ij == 15]
[(5, 2), (5, 4), (14, 1)]

Another Sage-specific data type that might interest you is sets -- these are like lists, but automatically condense duplicate entries. For example, the set of values in the matrix deg can be given by

sage: deg_values = set([x for row in deg for x in row])
sage: len(deg_values)