Using Python to set the colour of the largest sphere

I’ve got a project that is going to require something like this:

It’s easy enough to find the radii of the spheres, but I’m not sure if there is a fairly straightforward way of separating the largest from the smallest then applying the colour.

Any pointers would be appreciated. In the meantime I’m going to keep trying.

Thanks,

Dan

Thought I had it. I’ll keep trying.

Hi Dan

On my phone so pseudo code

for id in spheres:
    radius = get_radius(id)
    if radius >= divide : 
        rs.ObjectColor(id, large_color)
    else:
        rs.ObjectColor(id, small_color)

HTH
-Willem

Edit: I misread the question. If you want to find and colorize the largest only (and all the others different color) . You can color them all the smaller color and through @Dancergraham s sorting solution grab the largest and color that different.

Or if you have more than 2, put them in a list of tuples, then sort it:

mylist.append((radius, sphere_id))
mylist.sort()
largest = mylist[-1][1]
smallest = mylist[0][1]

Even better: largest = mylist.pop()[1] removes the final, largest item in the list and leaves all the rest so you can iterate through them. I like this solution because Python lists are optimised for quickly adding and removing items at the end.

Good one
Should that not be largest = mylist.pop(-1)[1]
Or am I missing something

-Willem

Either works actually! Pop() returns the tuple. Adding 1 or -1 takes the second (final) item from the tuple, ie the sphere itself :sunglasses:

I am being dense. Edited my answer already and edited again.

Does pop take the last item by default?

Yes, because lists are designed for LIFO access

1 Like

Thanks guys. I’m going to take another look first thing in the morning. Looks like some good tips here.

Dan

Hi @Willem, @Dancergraham,

This worked well. I had overlooked the sort function and the pop suggestion was very helpful too.

Thanks for your help,

Dan