-1

I have this code:

delIndex = []
for a in range(0, 10):
    if a not in found_index and len(found_index) > 0:
        delIndex.append(a)
for index in sorted(delIndex, reverse=True):
    del faces[index]

This code delete items who are not in the array found_index Is there a way simplify the code by using a method that do this job or another thing?

Kreumz
  • 103
  • 5
  • 1
    Not sure what exactly you are trying to do. you ask: "Is there a way simplify by using a method that do this job or another thing?" What is "another thing" in this context? What part of the above code exactly are you trying to simplify? Also, where is the `faces` dict coming from? What is "this job"? – Ryan Feb 27 '18 at 10:27
  • why the "numpy" tag ? – bruno desthuilliers Feb 27 '18 at 10:31
  • Can you guarantee that the array is unique? If you want to remove `1` from `[1,2,3,1]`, what do you expect? – erip Feb 27 '18 at 10:41
  • Check these links : [link 1](http://kitchingroup.cheme.cmu.edu/blog/2014/03/25/Deleting-multiple-elements-of-a-list/) [link 2](https://stackoverflow.com/a/36268815/9400024) they might help you to get info! – Abdullah Ahmed Ghaznavi Feb 27 '18 at 10:43
  • Welcome to SO! Please provide a **[mcve]**, as your question is currently unclear. – jpp Feb 27 '18 at 11:24

5 Answers5

0

I don't understand what faces is so I explain only how to remove elements by a difference of two sets of elements. In your case found_index and [a for a in range(0, 10)] . Use set built in in python, and the difference methods - documentation here:

values_to_remove = set([a for a in range(0, 10)])
found_index = set(found_index)
new_itemS = found_index.difference(values_to_remove)
# you can write also new_itemS = found_index - values_to_remove     new set with elements in found_index but not in values_to_remove
Lupanoide
  • 3,132
  • 20
  • 36
0

You can simplify the code to:

delIndex = sorted([a for a in range(0, 10) if a not in found_index], reverse=True)  
for index in delIndex:
    del faces[index]

If you update your answer with more details and samples of the list faces and found_index (e.g. is it unique, does it contain duplicates), and why you do a revers-sort, I may update this answer.

Edwin van Mierlo
  • 2,398
  • 1
  • 10
  • 19
0

If I understand correctly, that you want to filter out all elements from a list, which are contained in a second list:

[element for element in faces if element not in found_index]
guidot
  • 5,095
  • 2
  • 25
  • 37
0

You can use set for that

Try like this:-

a = [6,8,10] #to be deleted
b = list(range(11)) #compare list
ls= set(b) ^ set(a)
print(ls)#your output
Narendra
  • 1,511
  • 1
  • 10
  • 20
-3

I think you are looking for How to remove specific element in an array using python.

Let me know if this is what you are looking for. Alternatively provide us a sample of input and output so we can play with your code.

Update: Adding sample of code.

Sample of code:

#!/usr/bin/env python

mainArray = [1, 2, 3, 4, 5, 6]
arrayRemove = [1, 3, 6]

mainArray = [e for e in mainArray if e not in arrayRemove]
print(mainArray)
Thanos
  • 1,618
  • 4
  • 28
  • 49
  • 4
    this belongs to a comment, not an answer. – bruno desthuilliers Feb 27 '18 at 10:30
  • Hello @brunodesthuilliers, You know before voting down a new user that it is trying to assist for free his time it would better to teach how this forum works. Alternatively you end up pushing people out of spending their time to assist. (Keep that in mind for the next user that you will do the same) – Thanos Feb 27 '18 at 10:32
  • Since it is documented better provide a link in case that someone does not know where to read it. On top of that learn how to train to create better developers instead of train how to be wrong. – Thanos Feb 27 '18 at 10:48