Sorted arcpy.da.SearchCursor (sort of)

492
7
Jump to solution
10-09-2018 10:34 AM
JamesCrandall
MVP Frequent Contributor

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!

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

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']))
...                              
>>> 

View solution in original post

7 Replies
JoshuaBixby
MVP Esteemed Contributor

Just so I understand, your want a double sort using juncA, juncB?

0 Kudos
JamesCrandall
MVP Frequent Contributor

Yes, in the manner of the second output example above! 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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']))
...                              
>>> 
JamesCrandall
MVP Frequent Contributor

Yep!  Thank you!

0 Kudos
JamesCrandall
MVP Frequent Contributor

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"]))
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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.

0 Kudos
JamesCrandall
MVP Frequent Contributor

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.

0 Kudos