简体   繁体   中英

How to sort dictionary with multiple items

I found some similar questions about this, but suggested codes doesn´t work for my dictionary where I use tuple and I´m not good enough to edit them for my needs, it still gives me the errors. But I guess I need to use the lambda function somehow.

To clarify more the situation: I write a script for ANSA software, which selects separate PID regions of the model (each region consist of a number of elements) and then creates a connections between them. To decide which region should be connected with which I need to mark the regions with some numbers. The regions should be marked from 0 to some final number depending on their coordinates. So I think easiest way is to just sort the created dictionary as mentioned above. Then I would know if I select third item in dictionary, it would be third region of elements from the left side of the model.

The dictionary contains this:

{

(<Entity: 0x00007FF4261EE661: type: 513(513,N_CQUAD4) id:-354900>, <Entity: 0x00007FF4264AE648: type: 513(513,N_CQUAD4) id:-354901>, <Entity: 0x00007FF4264AD0A0: type: 513(513,N_CQUAD4) id:-355476>): [32.50000221162431, 73.00083277940169, 171.00023861627614], 

(<Entity: 0x00007FF4261EE662: type: 513(513,N_CQUAD4) id:-354901>, <Entity: 0x00007FF4264AE648: type: 513(513,N_CQUAD4) id:-354901>, <Entity: 0x00007FF4264AD0A0: type: 513(513,N_CQUAD4) id:-355476>): [51.50000221162431, 44.00083277940169, 125.00023861627614],

(<Entity: 0x00007FF4261EE660: type: 513(513,N_CQUAD4) id:-354902>, <Entity: 0x00007FF4264AE648: type: 513(513,N_CQUAD4) id:-354901>, <Entity: 0x00007FF4264AD0A0: type: 513(513,N_CQUAD4) id:-355476>): [11.50000221162431, 23.00083277940169, 106.00023861627614]

}

In the first, longer part of each item (the round brackets) is the list of elements with their properties. "Entity is some unique number, I actually have no idea for what it is used. "Type" says what shape element has, and "id" is obviously id of the element. I don´t know what more I could say about them.

The values in squared brackets at the end of each item are x,y,z coordinates. What I need is to sort the items of the dictionary according to the coordinates (not the order of coordinates.), First x. then z and then y? Could anyone help me with this?

I tried for example this, but I´m afraid it needs to be much more complicated than this:

region_position = sorted(region_position, key=lambda x: (x[1], x[3], x[2]))

I'd say the simplest way would be to create a new dictionary with the same keys and the proper ordering of x,z,y inside.

A one-liner would be (with oldDict the previous dictionary):

newDict = {k:[oldDict[k][0], oldDict[k][2], oldDict[k][1]] for k in oldDict}

Here, I am building a dictionary the same way I would a list-comprehension, iterating through each "k" key of the old dictionary, and assigning to each key a new list with the element in the order I want (here 0, 2 and 1, to re-order x, z and y).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM