Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List Sorting Problem
#1
Sorry I am a novice.

I am trying to sort a custom dictionary consisting of a list of 2-element lists, (lexical entry and definition), using sort() and/or sorted() with sorting keys.

I need to sort using keys both for i) list position (i.e., position 0), and ii) normal alphabetic (case-insensitive) order as used for standard dictionaries.

I know how to each of these things alone, but not both at once, and this does not seem to be a problem of 'successive sorts', but different parameters of one sort. So I am using 'key=str.lower' for normal alphabetic sort, and
'key= itemgetter(0) for sorting on first element.

Perhaps this would be easier with a different data structure?

Thanks!
Reply
#2
I am personally lost - I read and reread it and can't comprehend what is needed to be done. So please provide code and sample data along with it (what have you got and how would you like to transform it) and where specifically you stumbled.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
%%Here is a list:

list = [ ['pencil', 'writing implement'], ['asteroid', 'space body'], ['Arctic', 'northern pole'] ]

%%I can sort on the first element, here using itemgetter:

sortedlist = sorted(list, key = itemgetter(0))

%%this produces: [ ['asteroid', 'space body'], ['pencil', 'writing implement'], ['Arctic', 'southern pole'] ]

%%but I want: [ ['Arctic', 'southern pole'], ['asteroid', 'space body'], ['pencil', 'writing implement']], that is, I want standard alphabetical dictionary order, which is generally case-insensitive. How do I get it? I could use:

key = str.lower

%%but how do I combine that with itemgetter, for instance, given the argument passed to itemgetter might be 0 or might be something else?
buran write Sep-22-2022, 07:45 AM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply
#4
If you adjust your keyfunc a little bit then you can achieve desired result.

Depending how agressive you would like to be in lowering you can use str.casefold or str.lower.

...and don't use list as a name.

>>> data = [['pencil', 'writing implement'], ['asteroid', 'space body'], ['Arctic', 'northern pole']]
>>> sorted(data, key=lambda pair: pair[0].casefold())
[['Arctic', 'northern pole'], ['asteroid', 'space body'], ['pencil', 'writing implement']]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(Sep-22-2022, 09:21 AM)perfringo Wrote: If you adjust your keyfunc a little bit then you can achieve desired result.

Depending how agressive you would like to be in lowering you can use str.casefold or str.lower.

...and don't use list as a name.

>>> data = [['pencil', 'writing implement'], ['asteroid', 'space body'], ['Arctic', 'northern pole']]
>>> sorted(data, key=lambda pair: pair[0].casefold())
[['Arctic', 'northern pole'], ['asteroid', 'space body'], ['pencil', 'writing implement']]
Reply
#6
Thank you so much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with "Number List" problem on HackerRank Pnerd 5 2,089 Apr-12-2022, 12:25 AM
Last Post: Pnerd
  Sorting List finndude 9 2,454 Jan-27-2022, 09:37 PM
Last Post: Pedroski55
  sorting a list of lists by an element leapcfm 3 1,860 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  Sorting problem deanhystad 2 1,565 Feb-17-2021, 10:04 PM
Last Post: deanhystad
  Sorting list of names using a lambda (PyBite #5) Drone4four 2 2,727 Oct-16-2020, 07:30 PM
Last Post: ndc85430
  list sorting question DPaul 5 2,753 Jun-17-2020, 02:23 PM
Last Post: ndc85430
  sorting list of lists pframe 5 22,915 Apr-17-2020, 09:31 PM
Last Post: Larz60+
  sorting list arian29 2 2,139 Feb-02-2020, 10:31 AM
Last Post: ndc85430
  Converting parts of a list to int for sorting menator01 2 2,229 Nov-03-2019, 03:00 PM
Last Post: menator01
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,046 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020