Complex sort of a list of dicts by key: ascending and descending for every tuple of key's valueHow do I sort a list of dictionaries by a value of the dictionary?Sort a Map<Key, Value> by valuesHow to sort (list/tuple) of lists/tuples?Python sort list of lists / ascending and then decendingSort a list of lists with multiple keys and unknown ascend/descendsorted on basis of two keys, descending order sort for first and ascending for secondSorting a list of list of tuplessort list of lists in ascending and descending order with stringsUse of ordering to sort array of arrays, ascending and descendingSorting list of (integer,string) lists by first/descending and second/ascending element
NMaximize is not converging to a solution
Is it unprofessional to ask if a job posting on GlassDoor is real?
Character reincarnated...as a snail
Can I make popcorn with any corn?
Arrow those variables!
Maximum likelihood parameters deviate from posterior distributions
Find the result of this dual key cipher
If human space travel is limited by the G force vulnerability, is there a way to counter G forces?
How to draw a waving flag in TikZ
Was any UN Security Council vote triple-vetoed?
What's the output of a record needle playing an out-of-speed record
Could an aircraft fly or hover using only jets of compressed air?
What typically incentivizes a professor to change jobs to a lower ranking university?
Can a vampire attack twice with their claws using Multiattack?
Can you really stack all of this on an Opportunity Attack?
Convert two switches to a dual stack, and add outlet - possible here?
How old can references or sources in a thesis be?
Do infinite dimensional systems make sense?
What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)
LWC SFDX source push error TypeError: LWC1009: decl.moveTo is not a function
Paid for article while in US on F-1 visa?
"You are your self first supporter", a more proper way to say it
Watching something be written to a file live with tail
strTok function (thread safe, supports empty tokens, doesn't change string)
Complex sort of a list of dicts by key: ascending and descending for every tuple of key's value
How do I sort a list of dictionaries by a value of the dictionary?Sort a Map<Key, Value> by valuesHow to sort (list/tuple) of lists/tuples?Python sort list of lists / ascending and then decendingSort a list of lists with multiple keys and unknown ascend/descendsorted on basis of two keys, descending order sort for first and ascending for secondSorting a list of list of tuplessort list of lists in ascending and descending order with stringsUse of ordering to sort array of arrays, ascending and descendingSorting list of (integer,string) lists by first/descending and second/ascending element
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a list of dicts that I want to sort by its 'segment'-keys, which are lists of tuples:
example = ['segment': [(329, 363), (379, 397)], 'name': '1',
'segment': [(329, 336), (339, 341), (396, 399)], 'name': '2',
'segment': [(329, 363), (379, 399)], 'name': '3',
'segment': [(329, 336), (337, 357), (396, 399)], 'name': '4']
The sorted list should look like this:
sorted_example = ['segment': [(329, 363), (379, 399)], 'name': '3',
'segment': [(329, 336), (337, 357), (396, 399)], 'name': '4',
'segment': [(329, 336), (339, 341), (396, 399)], 'name': '2',
'segment': [(329, 363), (379, 397)], 'name': '1']
My desired output is sorted in a way, that I can easily check if dict[n+1] is fully contained in dict[n].
So first I want to sort ascending by the first element of the first tuple and then descending by the second element of the last tuple.
That's ok for cases like dict 1 and 3, but for other dicts like 2 and 4 I have to iterate over all tuples and sort ascending by its first element and descending by its second.
My code for achieving the first step is:
sorted_example = sorted(example, key=lambda k: (k['segment'][0][0], -k['segment'][-1][1]))
But I can't figure out how to iterate over all tuples in one list and sort them ascending and descending as described above.
I'd be very, very happy if someone could give me a hint!
python python-3.x sorting
add a comment |
I have a list of dicts that I want to sort by its 'segment'-keys, which are lists of tuples:
example = ['segment': [(329, 363), (379, 397)], 'name': '1',
'segment': [(329, 336), (339, 341), (396, 399)], 'name': '2',
'segment': [(329, 363), (379, 399)], 'name': '3',
'segment': [(329, 336), (337, 357), (396, 399)], 'name': '4']
The sorted list should look like this:
sorted_example = ['segment': [(329, 363), (379, 399)], 'name': '3',
'segment': [(329, 336), (337, 357), (396, 399)], 'name': '4',
'segment': [(329, 336), (339, 341), (396, 399)], 'name': '2',
'segment': [(329, 363), (379, 397)], 'name': '1']
My desired output is sorted in a way, that I can easily check if dict[n+1] is fully contained in dict[n].
So first I want to sort ascending by the first element of the first tuple and then descending by the second element of the last tuple.
That's ok for cases like dict 1 and 3, but for other dicts like 2 and 4 I have to iterate over all tuples and sort ascending by its first element and descending by its second.
My code for achieving the first step is:
sorted_example = sorted(example, key=lambda k: (k['segment'][0][0], -k['segment'][-1][1]))
But I can't figure out how to iterate over all tuples in one list and sort them ascending and descending as described above.
I'd be very, very happy if someone could give me a hint!
python python-3.x sorting
2
I do not still get the sorting logic. Why is dict 3 the first in the sorted list while dict1 the last?
– Onyambu
Mar 21 at 23:12
and what makes dict 4 to come before dict2?
– Onyambu
Mar 21 at 23:15
My first sorting step looks at the first element of the first tuple and sorts them ascending. Since all values here are the same there's no problem with that. Next the dicts are sorted in descending order by the second element of the last tuple, so the 397 of dict 1 makes the dict the last element, because every other second element of last tuples is greater than 397.
– Daniel MacDaniel
Mar 21 at 23:26
In my second step this sorting logic applies to every tuple of each list: So the 363 of the first tuple makes dict 3 the first dict (descending order for second tuple elements!), while the first element of the second tuple of dict 4 is lower than the first element of the second tuple of dict 2 (337 < 339) (ascending order for first tuple elements!)
– Daniel MacDaniel
Mar 21 at 23:31
add a comment |
I have a list of dicts that I want to sort by its 'segment'-keys, which are lists of tuples:
example = ['segment': [(329, 363), (379, 397)], 'name': '1',
'segment': [(329, 336), (339, 341), (396, 399)], 'name': '2',
'segment': [(329, 363), (379, 399)], 'name': '3',
'segment': [(329, 336), (337, 357), (396, 399)], 'name': '4']
The sorted list should look like this:
sorted_example = ['segment': [(329, 363), (379, 399)], 'name': '3',
'segment': [(329, 336), (337, 357), (396, 399)], 'name': '4',
'segment': [(329, 336), (339, 341), (396, 399)], 'name': '2',
'segment': [(329, 363), (379, 397)], 'name': '1']
My desired output is sorted in a way, that I can easily check if dict[n+1] is fully contained in dict[n].
So first I want to sort ascending by the first element of the first tuple and then descending by the second element of the last tuple.
That's ok for cases like dict 1 and 3, but for other dicts like 2 and 4 I have to iterate over all tuples and sort ascending by its first element and descending by its second.
My code for achieving the first step is:
sorted_example = sorted(example, key=lambda k: (k['segment'][0][0], -k['segment'][-1][1]))
But I can't figure out how to iterate over all tuples in one list and sort them ascending and descending as described above.
I'd be very, very happy if someone could give me a hint!
python python-3.x sorting
I have a list of dicts that I want to sort by its 'segment'-keys, which are lists of tuples:
example = ['segment': [(329, 363), (379, 397)], 'name': '1',
'segment': [(329, 336), (339, 341), (396, 399)], 'name': '2',
'segment': [(329, 363), (379, 399)], 'name': '3',
'segment': [(329, 336), (337, 357), (396, 399)], 'name': '4']
The sorted list should look like this:
sorted_example = ['segment': [(329, 363), (379, 399)], 'name': '3',
'segment': [(329, 336), (337, 357), (396, 399)], 'name': '4',
'segment': [(329, 336), (339, 341), (396, 399)], 'name': '2',
'segment': [(329, 363), (379, 397)], 'name': '1']
My desired output is sorted in a way, that I can easily check if dict[n+1] is fully contained in dict[n].
So first I want to sort ascending by the first element of the first tuple and then descending by the second element of the last tuple.
That's ok for cases like dict 1 and 3, but for other dicts like 2 and 4 I have to iterate over all tuples and sort ascending by its first element and descending by its second.
My code for achieving the first step is:
sorted_example = sorted(example, key=lambda k: (k['segment'][0][0], -k['segment'][-1][1]))
But I can't figure out how to iterate over all tuples in one list and sort them ascending and descending as described above.
I'd be very, very happy if someone could give me a hint!
python python-3.x sorting
python python-3.x sorting
edited Mar 22 at 0:28
blhsing
42.5k41743
42.5k41743
asked Mar 21 at 23:04
Daniel MacDanielDaniel MacDaniel
132
132
2
I do not still get the sorting logic. Why is dict 3 the first in the sorted list while dict1 the last?
– Onyambu
Mar 21 at 23:12
and what makes dict 4 to come before dict2?
– Onyambu
Mar 21 at 23:15
My first sorting step looks at the first element of the first tuple and sorts them ascending. Since all values here are the same there's no problem with that. Next the dicts are sorted in descending order by the second element of the last tuple, so the 397 of dict 1 makes the dict the last element, because every other second element of last tuples is greater than 397.
– Daniel MacDaniel
Mar 21 at 23:26
In my second step this sorting logic applies to every tuple of each list: So the 363 of the first tuple makes dict 3 the first dict (descending order for second tuple elements!), while the first element of the second tuple of dict 4 is lower than the first element of the second tuple of dict 2 (337 < 339) (ascending order for first tuple elements!)
– Daniel MacDaniel
Mar 21 at 23:31
add a comment |
2
I do not still get the sorting logic. Why is dict 3 the first in the sorted list while dict1 the last?
– Onyambu
Mar 21 at 23:12
and what makes dict 4 to come before dict2?
– Onyambu
Mar 21 at 23:15
My first sorting step looks at the first element of the first tuple and sorts them ascending. Since all values here are the same there's no problem with that. Next the dicts are sorted in descending order by the second element of the last tuple, so the 397 of dict 1 makes the dict the last element, because every other second element of last tuples is greater than 397.
– Daniel MacDaniel
Mar 21 at 23:26
In my second step this sorting logic applies to every tuple of each list: So the 363 of the first tuple makes dict 3 the first dict (descending order for second tuple elements!), while the first element of the second tuple of dict 4 is lower than the first element of the second tuple of dict 2 (337 < 339) (ascending order for first tuple elements!)
– Daniel MacDaniel
Mar 21 at 23:31
2
2
I do not still get the sorting logic. Why is dict 3 the first in the sorted list while dict1 the last?
– Onyambu
Mar 21 at 23:12
I do not still get the sorting logic. Why is dict 3 the first in the sorted list while dict1 the last?
– Onyambu
Mar 21 at 23:12
and what makes dict 4 to come before dict2?
– Onyambu
Mar 21 at 23:15
and what makes dict 4 to come before dict2?
– Onyambu
Mar 21 at 23:15
My first sorting step looks at the first element of the first tuple and sorts them ascending. Since all values here are the same there's no problem with that. Next the dicts are sorted in descending order by the second element of the last tuple, so the 397 of dict 1 makes the dict the last element, because every other second element of last tuples is greater than 397.
– Daniel MacDaniel
Mar 21 at 23:26
My first sorting step looks at the first element of the first tuple and sorts them ascending. Since all values here are the same there's no problem with that. Next the dicts are sorted in descending order by the second element of the last tuple, so the 397 of dict 1 makes the dict the last element, because every other second element of last tuples is greater than 397.
– Daniel MacDaniel
Mar 21 at 23:26
In my second step this sorting logic applies to every tuple of each list: So the 363 of the first tuple makes dict 3 the first dict (descending order for second tuple elements!), while the first element of the second tuple of dict 4 is lower than the first element of the second tuple of dict 2 (337 < 339) (ascending order for first tuple elements!)
– Daniel MacDaniel
Mar 21 at 23:31
In my second step this sorting logic applies to every tuple of each list: So the 363 of the first tuple makes dict 3 the first dict (descending order for second tuple elements!), while the first element of the second tuple of dict 4 is lower than the first element of the second tuple of dict 2 (337 < 339) (ascending order for first tuple elements!)
– Daniel MacDaniel
Mar 21 at 23:31
add a comment |
1 Answer
1
active
oldest
votes
You can use a generator expression that iterates over the tuples of a given sub-list in the dict, and returns a tuple of items with the second item negated so that it's sorted in descending order. Unpack the generator expression into the outputting tuple of the key function so that it's considered after the items placed for your first rule:
sorted(example, key=lambda k: (k['segment'][0][0], -k['segment'][-1][1], *((a, -b) for a, b in k['segment'])))
This returns:
['segment': [(329, 363), (379, 399)], 'name': '3',
'segment': [(329, 336), (337, 357), (396, 399)], 'name': '4',
'segment': [(329, 336), (339, 341), (396, 399)], 'name': '2',
'segment': [(329, 363), (379, 397)], 'name': '1']
1
Awesome, thanks a lot! I had only a vague idea of generator expressions before. This will simplyfie some other code of mine too!
– Daniel MacDaniel
Mar 22 at 11:07
What would be the right syntax if I want to sort the dicts only by the generator expression? (without thek['segment'][0][0], -k['segment'][-1][1]
) I tried many variations, but still get only syntax errors...
– Daniel MacDaniel
Mar 25 at 17:16
You can pass the generator expression to the tuple constructor instead in that case:sorted(example, key=lambda k: tuple((a, -b) for a, b in k['segment']))
– blhsing
Mar 25 at 17:49
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55290517%2fcomplex-sort-of-a-list-of-dicts-by-key-ascending-and-descending-for-every-tuple%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use a generator expression that iterates over the tuples of a given sub-list in the dict, and returns a tuple of items with the second item negated so that it's sorted in descending order. Unpack the generator expression into the outputting tuple of the key function so that it's considered after the items placed for your first rule:
sorted(example, key=lambda k: (k['segment'][0][0], -k['segment'][-1][1], *((a, -b) for a, b in k['segment'])))
This returns:
['segment': [(329, 363), (379, 399)], 'name': '3',
'segment': [(329, 336), (337, 357), (396, 399)], 'name': '4',
'segment': [(329, 336), (339, 341), (396, 399)], 'name': '2',
'segment': [(329, 363), (379, 397)], 'name': '1']
1
Awesome, thanks a lot! I had only a vague idea of generator expressions before. This will simplyfie some other code of mine too!
– Daniel MacDaniel
Mar 22 at 11:07
What would be the right syntax if I want to sort the dicts only by the generator expression? (without thek['segment'][0][0], -k['segment'][-1][1]
) I tried many variations, but still get only syntax errors...
– Daniel MacDaniel
Mar 25 at 17:16
You can pass the generator expression to the tuple constructor instead in that case:sorted(example, key=lambda k: tuple((a, -b) for a, b in k['segment']))
– blhsing
Mar 25 at 17:49
add a comment |
You can use a generator expression that iterates over the tuples of a given sub-list in the dict, and returns a tuple of items with the second item negated so that it's sorted in descending order. Unpack the generator expression into the outputting tuple of the key function so that it's considered after the items placed for your first rule:
sorted(example, key=lambda k: (k['segment'][0][0], -k['segment'][-1][1], *((a, -b) for a, b in k['segment'])))
This returns:
['segment': [(329, 363), (379, 399)], 'name': '3',
'segment': [(329, 336), (337, 357), (396, 399)], 'name': '4',
'segment': [(329, 336), (339, 341), (396, 399)], 'name': '2',
'segment': [(329, 363), (379, 397)], 'name': '1']
1
Awesome, thanks a lot! I had only a vague idea of generator expressions before. This will simplyfie some other code of mine too!
– Daniel MacDaniel
Mar 22 at 11:07
What would be the right syntax if I want to sort the dicts only by the generator expression? (without thek['segment'][0][0], -k['segment'][-1][1]
) I tried many variations, but still get only syntax errors...
– Daniel MacDaniel
Mar 25 at 17:16
You can pass the generator expression to the tuple constructor instead in that case:sorted(example, key=lambda k: tuple((a, -b) for a, b in k['segment']))
– blhsing
Mar 25 at 17:49
add a comment |
You can use a generator expression that iterates over the tuples of a given sub-list in the dict, and returns a tuple of items with the second item negated so that it's sorted in descending order. Unpack the generator expression into the outputting tuple of the key function so that it's considered after the items placed for your first rule:
sorted(example, key=lambda k: (k['segment'][0][0], -k['segment'][-1][1], *((a, -b) for a, b in k['segment'])))
This returns:
['segment': [(329, 363), (379, 399)], 'name': '3',
'segment': [(329, 336), (337, 357), (396, 399)], 'name': '4',
'segment': [(329, 336), (339, 341), (396, 399)], 'name': '2',
'segment': [(329, 363), (379, 397)], 'name': '1']
You can use a generator expression that iterates over the tuples of a given sub-list in the dict, and returns a tuple of items with the second item negated so that it's sorted in descending order. Unpack the generator expression into the outputting tuple of the key function so that it's considered after the items placed for your first rule:
sorted(example, key=lambda k: (k['segment'][0][0], -k['segment'][-1][1], *((a, -b) for a, b in k['segment'])))
This returns:
['segment': [(329, 363), (379, 399)], 'name': '3',
'segment': [(329, 336), (337, 357), (396, 399)], 'name': '4',
'segment': [(329, 336), (339, 341), (396, 399)], 'name': '2',
'segment': [(329, 363), (379, 397)], 'name': '1']
answered Mar 22 at 0:26
blhsingblhsing
42.5k41743
42.5k41743
1
Awesome, thanks a lot! I had only a vague idea of generator expressions before. This will simplyfie some other code of mine too!
– Daniel MacDaniel
Mar 22 at 11:07
What would be the right syntax if I want to sort the dicts only by the generator expression? (without thek['segment'][0][0], -k['segment'][-1][1]
) I tried many variations, but still get only syntax errors...
– Daniel MacDaniel
Mar 25 at 17:16
You can pass the generator expression to the tuple constructor instead in that case:sorted(example, key=lambda k: tuple((a, -b) for a, b in k['segment']))
– blhsing
Mar 25 at 17:49
add a comment |
1
Awesome, thanks a lot! I had only a vague idea of generator expressions before. This will simplyfie some other code of mine too!
– Daniel MacDaniel
Mar 22 at 11:07
What would be the right syntax if I want to sort the dicts only by the generator expression? (without thek['segment'][0][0], -k['segment'][-1][1]
) I tried many variations, but still get only syntax errors...
– Daniel MacDaniel
Mar 25 at 17:16
You can pass the generator expression to the tuple constructor instead in that case:sorted(example, key=lambda k: tuple((a, -b) for a, b in k['segment']))
– blhsing
Mar 25 at 17:49
1
1
Awesome, thanks a lot! I had only a vague idea of generator expressions before. This will simplyfie some other code of mine too!
– Daniel MacDaniel
Mar 22 at 11:07
Awesome, thanks a lot! I had only a vague idea of generator expressions before. This will simplyfie some other code of mine too!
– Daniel MacDaniel
Mar 22 at 11:07
What would be the right syntax if I want to sort the dicts only by the generator expression? (without the
k['segment'][0][0], -k['segment'][-1][1]
) I tried many variations, but still get only syntax errors...– Daniel MacDaniel
Mar 25 at 17:16
What would be the right syntax if I want to sort the dicts only by the generator expression? (without the
k['segment'][0][0], -k['segment'][-1][1]
) I tried many variations, but still get only syntax errors...– Daniel MacDaniel
Mar 25 at 17:16
You can pass the generator expression to the tuple constructor instead in that case:
sorted(example, key=lambda k: tuple((a, -b) for a, b in k['segment']))
– blhsing
Mar 25 at 17:49
You can pass the generator expression to the tuple constructor instead in that case:
sorted(example, key=lambda k: tuple((a, -b) for a, b in k['segment']))
– blhsing
Mar 25 at 17:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55290517%2fcomplex-sort-of-a-list-of-dicts-by-key-ascending-and-descending-for-every-tuple%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
I do not still get the sorting logic. Why is dict 3 the first in the sorted list while dict1 the last?
– Onyambu
Mar 21 at 23:12
and what makes dict 4 to come before dict2?
– Onyambu
Mar 21 at 23:15
My first sorting step looks at the first element of the first tuple and sorts them ascending. Since all values here are the same there's no problem with that. Next the dicts are sorted in descending order by the second element of the last tuple, so the 397 of dict 1 makes the dict the last element, because every other second element of last tuples is greater than 397.
– Daniel MacDaniel
Mar 21 at 23:26
In my second step this sorting logic applies to every tuple of each list: So the 363 of the first tuple makes dict 3 the first dict (descending order for second tuple elements!), while the first element of the second tuple of dict 4 is lower than the first element of the second tuple of dict 2 (337 < 339) (ascending order for first tuple elements!)
– Daniel MacDaniel
Mar 21 at 23:31