Convert 2 Lists into a Dictionary

Hi all,

I am trying to sort panels’ sequence by the distance of panel to a specific pt.
However I have never use dictionary before.
I have no idea how to create dictionary.
Now I have a list of panels’ guid and a list of distance value.
Is there any way to convert 2 lists into a dictionary?
Or there is another more easier way to create a dictionary?

Here is my code:

import rhinoscriptsyntax as rs

Ref_pt = rs.GetPoint(“Pick a pt for sorting sequence”) #pt for distance checking
panels = rs.GetObjects(“Pick panels to assign name”,8,True) #get all panels’ id

dists = []
for panel in panels: #calculate the distance between the reference pt and every panel
ctrPt = rs.SurfaceAreaCentroid(panel) #Find panel centroid
dist = rs.Distance(Ref_pt,ctrPt) #check distance
dists.append(dist)

sorted_panels = sorted(panels,key=dists) #sort panel sequence by the distance to the ref pt
for panel in sorted_panels:
rs.SelectObject(panel)
rs.Command("_SelNone")

Appreciate for any help or suggestions.
Thank you in advance.

Jack

HI Jack_Zeng
You can create a dictionary like this

Thanks!
In that case, could you also instruct how can I sort the panels using this dictionary?
And how can I extract panels back after the sort?

Cheers,

J

If you want to sort the panels by distance, I would suggest you don’t necessarily need a dictionary…

Use the zip function as above to zip each panel ID with its distance, the distance being the first index of the tuple. Then sort the resulting list. The panels will get sorted synchronously with the distances… Something like:

idList=['panel_zero','panel_one','panel_two','panel_three','panel_four']
distList=[5.2,3.5,10.8,1.0,6.8]

master=zip(distList,idList)
print master
master.sort()
print master
print list(zip(*master)[1])

>>>[(5.2, 'panel_zero'), (3.5, 'panel_one'), (10.8, 'panel_two'), (1.0, 'panel_three'), (6.8, 'panel_four')]
>>>[(1.0, 'panel_three'), (3.5, 'panel_one'), (5.2, 'panel_zero'), (6.8, 'panel_four'), (10.8, 'panel_two')]
>>>['panel_three', 'panel_one', 'panel_zero', 'panel_four', 'panel_two']

You can also use the dictionary and the sorted list of distances to get the sorted list of panels:

idList=['panel_zero','panel_one','panel_two','panel_three','panel_four']
distList=[5.2,3.5,10.8,1.0,6.8]
dic={a:b for a,b in zip(distList,idList)}
distList.sort()
panels_sorted=[dic[i] for i in distList]
print distList
print panels_sorted

>>>[1.0, 3.5, 5.2, 6.8, 10.8]
>>>['panel_three', 'panel_one', 'panel_zero', 'panel_four', 'panel_two']


How to do this in vb.net? anyone knows xD or is zip a python only function? ^^

  • cheers

Wow, cool!
Thank you Helvetosaur!!

There is a Zip function in .net, in the LINQ extensions.

Do you know some website I can find that can explain functions like zip?
Don’t know what does zip function do.
Don’t think I can get help on the rhinopython help file…

https://docs.python.org/3/

I’m pretty certain that IronPython is still on 2.7, so: https://docs.python.org/2.7/ Just in case :wink:

Yes, correct, just a bit hasty on the copy and paste from my phone…
Thanks, --Mitch

Cool~
Thank you Helvetosaur and Anders~