Grasshopper

algorithmic modeling for Rhino

Hello everyone,

I try to script a python component which can cull a duplicate object inside a list.
I actually don't want to delete the duplicated object, but the one which with it was checked (I hope I am clear enough.)
I wrote this forloop:


for i in range(len(ListToModify)):
    if PointToCheck[0] == ListToModify[i]:
        #print ListToModify[i]
        ListToModify.pop(ListToModify[i])

But I got a message error : expected int, got Point3d

I also tried with .remove or with rs.DeleteObject, but I couldn't make it work.

Maybe it also possible to do it in grasshopper, please tell me.

Thanks,


Roger

Views: 2648

Replies to This Discussion

Hi Roger,

Which line is giving the error? Also, shouldn't you be getting an index out of range error since you are shortening the list while looping over it?

Instead of:

ListToModify.pop(ListToModify([i]))

try:

ListToModify.pop(i)

But perhaps a better solution would be to use an ordered dictionary:

from collections import OrderedDict

original = [1,3,2,5,3,1,4,6,2,5,66]
dup_original = original[:]

d = OrderedDict.fromkeys(dup_original)
culled_list = list(d)
print culled_list

Great! [:] is called slicing and is one method of copying a list. You can do things like [:-1] which means copying everything except the last item. The problem of removing duplicates in a list is quite common. A quick search on stack overflow suggested using an OrderedDictionary as an elegant way to solve this. OrderedDictionay can be found in the Python Collections module.

See https://docs.python.org/2/library/collections.html

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service