Questions tagged [indices]

Use this tag for questions about specifying or selecting the correct information from a structure using indices.

For any set of records, indices are identifiers of which specify information of the address of each record.

Check the Wikipedia page for arrays to know more.

882 questions
581
votes
18 answers

How to find all occurrences of an element in a list

index() will give the first occurrence of an item in a list. Is there a neat trick which returns all indices in a list for an element?
Bruce
  • 33,927
  • 76
  • 174
  • 262
212
votes
12 answers

Access lapply index names inside FUN

Is there a way to get the list index name in my lapply() function? n = names(mylist) lapply(mylist, function(list.elem) { cat("What is the name of this list element?\n" }) I asked before if it's possible to preserve the index names in the lapply()…
Robert Kubrick
  • 8,413
  • 13
  • 59
  • 91
152
votes
5 answers

How to get the index of a maximum element in a NumPy array along one axis

I have a 2 dimensional NumPy array. I know how to get the maximum values over axes: >>> a = array([[1,2,3],[4,3,1]]) >>> amax(a,axis=0) array([4, 3, 3]) How can I get the indices of the maximum elements? I would like as output array([1,1,0])…
Peter Smit
  • 27,696
  • 33
  • 111
  • 170
52
votes
2 answers

Indexing a numpy array with a list of tuples

Why can't I index an ndarray using a list of tuple indices like so? idx = [(x1, y1), ... (xn, yn)] X[idx] Instead I have to do something unwieldy like idx2 = numpy.array(idx) X[idx2[:, 0], idx2[:, 1]] # or more…
Emre
  • 5,976
  • 7
  • 29
  • 42
49
votes
9 answers

Proper terminology, should I say indexes or indices?

I had a question about indices on a table and I put it up on Stack Overflow. I got my answer, but someone changed the word indices to say indexes. We know that the plural of Index is Indices, but we also know that almost everybody prefers the…
Raj More
  • 47,048
  • 33
  • 131
  • 198
44
votes
4 answers

Splitting a string by list of indices

I want to split a string by a list of indices, where the split segments begin with one index and end before the next one. Example: s = 'long string that I want to split up' indices = [0,5,12,17] parts = [s[index:] for index in indices] for part in…
Yarin
  • 173,523
  • 149
  • 402
  • 512
40
votes
5 answers

What is the order of evaluation in python when using pop(), list[-1] and +=?

a = [1, 2, 3] a[-1] += a.pop() This results in [1, 6]. a = [1, 2, 3] a[0] += a.pop() This results in [4, 2]. What order of evaluation gives these two results?
Simd
  • 19,447
  • 42
  • 136
  • 271
36
votes
7 answers

Find the indices of elements greater than x

Given the following vector, a = [1, 2, 3, 4, 5, 6, 7, 8, 9] I need to identify the indices of "a" whose elements are >= than 4, like this: idx = [3, 4, 5, 6, 7, 8] The info in "idx" will be used to delete the elements from another list X (X has…
Oliver Amundsen
  • 1,491
  • 2
  • 21
  • 40
35
votes
3 answers

In TensorFlow, how can I get nonzero values and their indices from a tensor with python?

I want to do something like this. Let's say we have a tensor A. A = [[1,0],[0,4]] And I want to get nonzero values and their indices from it. Nonzero values: [1,4] Nonzero indices: [[0,0],[1,1]] There are similar operations in…
ByungSoo Ko
  • 503
  • 1
  • 6
  • 7
34
votes
1 answer

Apply a function to a subset of data.table columns, by column-indices instead of name

I'm trying to apply a function to a group of columns in a large data.table without referring to each one individually. a <- data.table( a=as.character(rnorm(5)), b=as.character(rnorm(5)), c=as.character(rnorm(5)), …
Tahnoon Pasha
  • 5,848
  • 14
  • 49
  • 75
32
votes
3 answers

Can anybody explain me the numpy.indices()?

I've read documentation several times about np.indices() but I can't seem to grasp what is it about. I've used it numerous times on things to see what it does, but I still can't really get it. Maybe the thing is I'm a beginner in programming so…
lkky7
  • 557
  • 1
  • 4
  • 10
30
votes
3 answers

Python equivalent of which() in R

I am trying to take the following R statement and convert it to Python using NumPy: 1 + apply(tmp,1,function(x) length(which(x[1:k] < x[k+1]))) Is there a Python equivalent to which()? Here, x is row in matrix tmp, and k corresponds to the number…
Bokononisms
  • 349
  • 1
  • 3
  • 8
28
votes
6 answers

How to efficiently retrieve the indices of maximum values in a Torch tensor?

Assume to have a torch tensor, for example of the following shape: x = torch.rand(20, 1, 120, 120) What I would like now, is to get the indices of the maximum values of each 120x120 matrix. To simplify the problem I would first x.squeeze() to work…
Chris
  • 1,266
  • 4
  • 16
  • 34
22
votes
2 answers

Using list elements and indices together

I've always found it awkward to have a function or expression that requires use of the values, as well as indices, of a list (or array, applies just the same) in Haskell. I wrote validQueens below while experimenting with the N-queens problem here…
jon_darkstar
  • 16,398
  • 7
  • 29
  • 37
21
votes
2 answers

TypeError: list indices must be integers or slices, not list

array = some kind of list with 3 columns and unlimited amount of rows with data inside of it. Volume = array[0][2] counter = 0 for i in array: if Volume == array[i][2]: #<------ why is this line a problem? counter += 1
Michael
  • 223
  • 1
  • 2
  • 8
1
2 3
58 59