I just can't stand Stackexchange so asking some familiar folks around here for some assistance with rearranging a dictionary that I'm building from an arcpy.da.SearchCursor. Not really arcpy-specific that I need help with -- what I need is to take this dictionary:
{
"junctionMatrix": [
{
"WCU": "STA1E_CELL_7",
"juncA": "S-1A",
"juncB": "S-1B",
"juncDistance": 5475.69
},
{
"WCU": "STA1E_CELL_7",
"juncA": "S-1B",
"juncB": "S-1A",
"juncDistance": 5475.69
},
{
"WCU": "STA1E_CELL_7",
"juncA": "S-1A",
"juncB": "S-1C",
"juncDistance": 8140.32
},
{
"WCU": "STA1E_CELL_7",
"juncA": "S-1C",
"juncB": "S-1A",
"juncDistance": 8140.32
},
{
"WCU": "STA1E_CELL_7",
"juncA": "S-1B",
"juncB": "S-1C",
"juncDistance": 6891.71
},
{
"WCU": "STA1E_CELL_7",
"juncA": "S-1C",
"juncB": "S-1B",
"juncDistance": 6891.71
}
]
}
...and rearrange so that values are sorted in this manner:
{
"junctionMatrix": [
{
"WCU": "STA1E_CELL_7",
"juncA": "S-1A",
"juncB": "S-1B",
"juncDistance": 5475.69
},
{
"WCU": "STA1E_CELL_7",
"juncA": "S-1A",
"juncB": "S-1C",
"juncDistance": 6891.71
},
{
"WCU": "STA1E_CELL_7",
"juncA": "S-1B",
"juncB": "S-1A",
"juncDistance": 5475.69
},
{
"WCU": "STA1E_CELL_7",
"juncA": "S-1B",
"juncB": "S-1C",
"juncDistance": 6891.71
},
{
"WCU": "STA1E_CELL_7",
"juncA": "S-1C",
"juncB": "S-1A",
"juncDistance": 8140.32
},
{
"WCU": "STA1E_CELL_7",
"juncA": "S-1C",
"juncB": "S-1B",
"juncDistance": 6891.71
}
]
}
Any suggestions are appreciated!
Solved! Go to Solution.
Try:
d = {
... "junctionMatrix": [
... {
... "WCU": "STA1E_CELL_7",
... "juncA": "S-1A",
... "juncB": "S-1B",
... "juncDistance": 5475.69
... },
... {
... "WCU": "STA1E_CELL_7",
... "juncA": "S-1B",
... "juncB": "S-1A",
... "juncDistance": 5475.69
... },
... {
... "WCU": "STA1E_CELL_7",
... "juncA": "S-1A",
... "juncB": "S-1C",
... "juncDistance": 8140.32
... },
... {
... "WCU": "STA1E_CELL_7",
... "juncA": "S-1C",
... "juncB": "S-1A",
... "juncDistance": 8140.32
... },
... {
... "WCU": "STA1E_CELL_7",
... "juncA": "S-1B",
... "juncB": "S-1C",
... "juncDistance": 6891.71
... },
... {
... "WCU": "STA1E_CELL_7",
... "juncA": "S-1C",
... "juncB": "S-1B",
... "juncDistance": 6891.71
... }
... ]
... }
...
>>>
>>> d["junctionMatrix"] = sorted(d["junctionMatrix"],
... key=lambda x: (x['juncA'],x['juncB']))
...
>>>
Just so I understand, your want a double sort using juncA, juncB?
Yes, in the manner of the second output example above!
Try:
d = {
... "junctionMatrix": [
... {
... "WCU": "STA1E_CELL_7",
... "juncA": "S-1A",
... "juncB": "S-1B",
... "juncDistance": 5475.69
... },
... {
... "WCU": "STA1E_CELL_7",
... "juncA": "S-1B",
... "juncB": "S-1A",
... "juncDistance": 5475.69
... },
... {
... "WCU": "STA1E_CELL_7",
... "juncA": "S-1A",
... "juncB": "S-1C",
... "juncDistance": 8140.32
... },
... {
... "WCU": "STA1E_CELL_7",
... "juncA": "S-1C",
... "juncB": "S-1A",
... "juncDistance": 8140.32
... },
... {
... "WCU": "STA1E_CELL_7",
... "juncA": "S-1B",
... "juncB": "S-1C",
... "juncDistance": 6891.71
... },
... {
... "WCU": "STA1E_CELL_7",
... "juncA": "S-1C",
... "juncB": "S-1B",
... "juncDistance": 6891.71
... }
... ]
... }
...
>>>
>>> d["junctionMatrix"] = sorted(d["junctionMatrix"],
... key=lambda x: (x['juncA'],x['juncB']))
...
>>>
Yep! Thank you!
The closest I've gotten is to take the data = {} that is populated from my SearchCursor and reset it with:
data = sorted(data["junctionMatrix"], key=lambda x: (x["juncA"], x["juncB"]))
I see I suggested what you already tried. In terms of sorting, the syntax we both used works, so I am confused what isn't working for you.
I was close, you nailed the complete solution --- I was not correctly setting the "data" variable and get it under the "junctionMatrix" attribute. Pretty simple misunderstanding on my part!
Thanks again.