Deutsch English Français Italiano |
<v4v2br$22c0m$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Malcolm McLean <malcolm.arthur.mclean@gmail.com> Newsgroups: comp.lang.c Subject: Re: Baby X is bor nagain Date: Wed, 19 Jun 2024 17:52:11 +0100 Organization: A noiseless patient Spider Lines: 88 Message-ID: <v4v2br$22c0m$1@dont-email.me> References: <v494f9$von8$1@dont-email.me> <v49seg$14cva$1@raubtier-asyl.eternal-september.org> <v49t6f$14i1o$1@dont-email.me> <v4bcbj$1gqlo$1@raubtier-asyl.eternal-september.org> <v4bh56$1hibd$1@dont-email.me> <v4c0mg$1kjmk$1@dont-email.me> <v4c8s4$1lki1$4@dont-email.me> <20240613002933.000075c5@yahoo.com> <v4emki$28d1b$1@dont-email.me> <20240613174354.00005498@yahoo.com> <v4okn9$flpo$2@dont-email.me> <20240617002924.597@kylheku.com> <v4pddb$m5th$1@dont-email.me> <20240618115650.00006e3f@yahoo.com> <v4rv0o$1b7h1$1@dont-email.me> <20240618184026.000046e1@yahoo.com> <v4sd75$1ed31$1@dont-email.me> <877celzx14.fsf@nosuchdomain.example.com> <v4u85k$1t2pu$2@dont-email.me> <v4ucmn$1u14i$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Wed, 19 Jun 2024 18:52:11 +0200 (CEST) Injection-Info: dont-email.me; posting-host="1d7282bfc8cc6ef119aa4ef8aa4826c3"; logging-data="2174998"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/hMCcwMWKPEGzwx942uTOrU4oefUjAxdY=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:TIOuH5oXFfugBED8fX8LuKzcBM0= Content-Language: en-GB In-Reply-To: <v4ucmn$1u14i$1@dont-email.me> Bytes: 4963 On 19/06/2024 11:42, David Brown wrote: > On 19/06/2024 11:25, Malcolm McLean wrote: >> On 18/06/2024 23:49, Keith Thompson wrote: >>> Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes: >>> [...] >>>> And it didn't take long to get Python to sort the list alphabetically, >>>> but there seemed no way in to the sort comparision function >>>> itself. And I had to give up. >>> >>> <OT> >>> https://docs.python.org/3/library/functions.html#sorted >>> https://docs.python.org/3/library/stdtypes.html#list.sort >>> </OT> >>> >> key specifies a function of one argument that is used to extract a >> comparison key from each element in iterable (for example, >> key=str.lower). The default value is None (compare the elements >> directly). >> >> You see the problem. I can sort on any field. I can sort alphaetically >> upwards and downwards. But I don't want to do that. I want to use a >> non-alphabetical comaprison function on two fields, and I need to >> specify that myself, because it's impossible that it is available >> anywhere. And that is to sort alphalbetically, except where the >> strings match except for an emedded "_L_" or "_D_" where the string >> wth the embedded "L" shoud be treated as closer to A than the string >> with the emebdded "_D_". >> > > def LD_key(n) : > if "_L" in n : return (0, n) > if "_D_" in n : return (1, n) > return (2, n) > > Now you have a key function that will put all names containing "_L_" > first, then all names containing "_D_", then everything else, with > alphabetic sorting within those groups. > Yes, but that's not quite what we want. A typical input would go. quill_icon_D_.png quill_icon_D_@2x.png quill_icon_L_.png quill_icon_L_@2x.png aardvark.png zebra.png and the ouput we want is aardvark.png quill_icon_L_.png quill_icon_D_.png quill_icon_L_@2x.png quill_icon_D_@2x.png zebra.png > There is no problem here - you just have to think about things in a > different way. > > (I don't know why Python 3 dropped the comparison function support from > sort()/sorted(). It might be that a key function is more efficient, > since you call it once for each item rather than once for each comparison.) > >> And I'm sure there is some way to achiev e this. But in C, it s >> achieved simply by declaring that qsort takes a function pointer to >> user-supplied code. >> > > Yes, there is some way to achieve this all in Python. And like pretty > much every other question that is commonly asked, google will tell you > the answer. Sometimes things seem hard - then you do a little research, > learn a bit, and then its easy. > > I struugled with it for a while and, as you say, had recourse to the web. It was the very first Python program I tried to write, and it semmed unacceptably difficult to carry out a simple customised sort. I got alphabetical sorting going quite easily, but you don't need scripting for that. It wasn't what I was ment to be spending my time on anyway. So I had to drop the idea of automating the collation of resources with a Python script. -- Check out my hobby project. http://malcolmmclean.github.io/babyxrc