Grasshopper

algorithmic modeling for Rhino

Hi,

I'm having problem with the logic in a script I'm writing.

I have a list [1,2,3,4,4,5,5,5,6,7] and I want to remove all the values that duplicate so the list ends up like this [1,2,3,6,7].

I'm coding in python but any pseudocode would be appretiate.

Thanks,

M

Views: 642

Replies to This Discussion

Hi Florian,

That simplifies the list. What i want to do is if a number gets repeated remove both numbers from the list. eg. [1,2,3,3,3] >> [1,2]

 

it is a bit of a workaround but I managed to do it.

Any other ideas are welcome.

this is what i did:

trial = [1,2,3,4,5,6,7,3,4,5,6,8,9,10]
newTrial = sorted(trial)
print newTrial
list = []
listLength = len(newTrial)

for i in range(0,25):
for j in range(0,listLength):
if i == newTrial[j]:
if j == 0:
if newTrial[j] != newTrial[j+1]:
list.append(newTrial[j])
elif j == listLength-1:
if newTrial[j] != newTrial[j-1]:
list.append(newTrial[j])

else:
if newTrial[j] != newTrial[j+1] and newTrial[j] != newTrial[j-1]:
print newTrial[j]
list.append(newTrial[j])


print newTrial[j]
list.append(newTrial[j])

thanks,

M

You could use Python sets for this, like so:

myList = [1,2,3,4,4,5,5,5,6,7]

myListCullDup = list(set(myList))

Just understood what you're trying to do. How about something like this (untested):

inputList= [1,2,3,4,4,5,5,5,6,7]
outputList = []
for item in inputList:
    if inputList.count(item) == 1:
        outputList.append(item)

Just tested the code above and it works as expected. It could probably be optimized if you're running it on very large lists with lots of duplicate values. Also, here's an even simpler version using list comprehension:

inputList = [1,2,3,4,4,5,5,5,6,7]
outputList = [i for i in inputList if inputList.count(i) == 1]


Can't really beat one line of code as far as simplicity goes :)

Hi Andres,

That is exactly what I needed.

Thanks,

M

Cool, glad it helped..

Three approaches that spring to mind:

  1. Iterate over all items in the collection (except the last one). Then iterate over all subsequent items. If two of these items match then you have a duplicate value, cache this value. Iterate over all items in the collection and remove them if they equal the cache.
  2. Iterate over all items in the collection. Iterate over all other items in the collection. If there is no equality, copy the item into a new list.
  3. Iterate over all items in the collection (except the last one). Then iterate over all subsequent items. If the subsequent item matches the current item, remove the subsequent item and set a deletion boolean. When you're done iterating over all subsequent items, check the deletion bool and remove the current item if it's true.

(1) in VB:

For i As Int32 = 0 To list.Count - 2

  If (i >= list.Count-1) Then Exit For

  Dim cache As Int32 = list(i)

  Dim delete As Boolean = False

  For j As Int32 = i+1 To list.Count - 1

    If (list(i) = list(j)) Then

      delete = True

      Exit For

    End If

  Next

  If (delete) Then

    For j As Int32 = list.Count - 1 To 0 Step -1

      If (list(j) = cache) Then

        list.RemoveAt(j)

    Next

  End If

Next

(3) in C#:

for (int i = 0; i < list.Count; i++)

{

  bool deleteItem = false;

  for (int j = list.Count-1; j > i; j--)

  {

    if (list[i] = list[j])

    {

      list.RemoveAt(j);

      deleteItem = true;

    }

  }

  if (deleteItem)

    list.RemoveAt(i);

}

There are all kinds of clever tricks you can use with Linq and I'm sure that python has some clever list handling as well. But from a pure no-tricks and no-optimizations point of view the above would be my first approach.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Miguel,

I'm looking for the same, do you have a GH module you could share?

Anton

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service