1

I want to create a new_list which will contain only the items of my old_list which satisfy the condition that the index in an array of labels is 3. I am trying something like this:

new_list = [x for x in old_list if idx[x] == 3]
IndexError: arrays used as indices must be of integer (or boolean) type

But i am getting the following error because idx is an array. How can i solve this problem?

edited: Idx is an array of equal size with my original data which contains labels for them. So basically i want to create a new list which will contain only the items of my original list which e.g have the label 3.

I want to do something like this: cluster_a = [old_list[x] for x in idx if x == 3]

Clarification: my old list is a list containing 3d arrays and the idx is an equal size array containing a label for each 3d array of my list as i aforementioned. I am trying my best to explain the problem. If something is needed please tell me.

This is the list with the 3d arrays

and this is the array with the labels

6
  • what you mean by index in an array of labels is 3 ? what index ? can you show an example ? Commented Nov 8, 2014 at 17:52
  • i mean the containing value Commented Nov 8, 2014 at 17:52
  • Can you show us what old_list looks like? Commented Nov 8, 2014 at 17:53
  • 1
    And why do you have to do it with list comprehension? By making it 3 lines longer this will become a very easy task, and at the same time code readability will skyrocket. Commented Nov 8, 2014 at 17:53
  • Code readability will probably not be an issue if the correct code is written. Commented Nov 8, 2014 at 17:55

2 Answers 2

1

The problem is not that idx is a list but probably that x is an array - old_list must contain a list as an element. You need to reference the index, and not the item itself:

[old_list[x] for x in range(len(old_list)) if idx[x] == 3]

here's a minimal example:

>>> old_list = [4,5,6]
>>> idx = [3,2,3]
>>> [old_list[x] for x in range(len(old_list)) if idx[x] == 3]
[4, 6]
Sign up to request clarification or add additional context in comments.

9 Comments

This identifies the (likely) problem, but does not provide a solution.
The solution in this case is to rethink the logic. Its impossible to tell what can be done differently with the code exposed.
Which is why you need clarification of the actual problem before providing an answer.
@WeaselFox which would mean that, currently, there is no possible answer and we should probably flag the question appropriately and/or ask in comments. But this isn't really an answer.
@Puciek - I dont agree, but in any case the question was updated, so that irrelevant.
|
0

What about this ? :

new_list = [x for x in old_list if idx.index(x) == 3 ]

2 Comments

when i try that i get an AttributeError:'numpy.ndarray' object has no attribute 'index'
idx.index(x) will return the first index in idx where the value is x, but according to the OP, idx is a list of labels, so I dont think this is right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.