Python list comparison

While comparing a list of hundred distances to one distance, the < only gives one true or false.
My goal is to select the indexnumbers/items within the given range of z.

python list comparison 01.gh (13.9 KB)

Hi Edward,

the x variable comes in a s a list, so you need to itterate through it’s values and compare each value individually
see this quick hack where I use the print output to dispach the points:

-Willem

1 Like

For the next step I could also use for in range again. However, itemgetter is another option. In sublimetext it works when I replace the list of indices with numbers, but it would not work with a list.
I think I implement it wrong.
python list comparison 02.gh (15.4 KB)

Hi Edward,

I’m not familiar with itemgetter, but my take would be:

import rhinoscriptsyntax as rs
from operator import itemgetter


emplist = []
emplist.append(y[0])
b = rs.AddCircle(y[-1],z)



distances = []
dispatch_smaller = []
dispatch_equalorlarger = []

#handle everything in one for loop
for i,xval in enumerate(x):
    dist = rs.Distance(y[-1],xval)
    if (dist<z):
        dispatch_smaller.append(x[i])
    else:
        dispatch_equalorlarger.append(x[i])


a = dispatch_smaller
b = dispatch_equalorlarger

-Willem

1 Like

Hi there! I’m reviving this topic since I’m stuck in a similar problem, and I don’t get what I’m doing wrong.

I have a list that has several branches, each branch has 3 indices, I want to compare each branch so the values in the indices 1 and 2 are checked, if the two of them are repeated in another branch, then the complete branch will be placed in a list called “branches_duplicate”. If they don’t or are the first occurrence, they are to be placed in a list called “branches_unique”.

Until now it seems that I can print two lists, but they seem to be empty.

Here is my code:

# Provided list of branches
branches = []  # List of values from Model
branches_unique = []  # List to store branches with unique values at indices 1 and 2
branches_duplicate = []  # List to store branches with repeating values at indices 1 and 2

# Iterate through each branch
for branch in branches:
    seen_values = set()  # Keep track of unique values encountered in the current branch
    duplicate = False  # Flag to indicate if the branch has duplicate values
    
    # Check each element in the branch
    for i in range(1, 3):  # Check indices 1 and 2
        if branch[i] in seen_values:
            duplicate = True
            break
        else:
            seen_values.add(branch[i])
    
    # Append the branch to the appropriate list
    if duplicate:
        branches_duplicate.append(branch)
    else:
        branches_unique.append(branch)

print(branches_unique)
print(branches_duplicate)