Python Forum
List of pathlib.Paths Not Ordered As Same List of Same String Filenames
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List of pathlib.Paths Not Ordered As Same List of Same String Filenames
#11
Quote:phrase
In my book means 'in my opinion' or 'according to my beliefs'.
The greatest manager there has ever been, or ever will be in my book, is retiring.
Synonyms: in my opinion, to me, as far as I am concerned   More Synonyms of in my book

Sorry, I have written magazine articles, but not a book yet (have one with 350 pages, but got burnt out)
Reply
#12
Sorry, English is not my native language.
Reply
#13
Not a problem. When I went to work in England and went out to a workplace pub for a beer with the guys,
after we had a couple, one stated 'I'm pissed'.
In USA, that means angry.
In England, intoxicated.
I didn't understand how he could be 'pissed' and laughing at the same time.
Reply
#14
Thanks, @heiner55 and @larz6+. However, I still think that comparing pathlib.Path is flawed if it cannot provide a alphabetical order equal to string order of corresponding filenames. In most cases, this is not a problem for sure.
Reply
#15
Looking at this more closely, I think there is credibility to your argument,
enough so that the author of pathlib Antoine Pitrou should be asked.
His github account is: https://github.com/pitrou
Reply
#16
Ok, I do that. Thank you.
Reply
#17
(Nov-11-2017, 05:54 PM)QbLearningPython Wrote: I supposed that the order retrieved by sorted() a list of Paths would be the same as the order retrieved by sorted() a list of their (string) filenames.

The problems comes from the fact that you assume certain behaviour, i.e. the sort is based on the full path as string. However if you look at the code of pathlib, you will see it is based on tuple of path parts.
https://bitbucket.org/pitrou/pathlib/src...ew-default
look at lines 827-869
this example mimics the behaviour
a = ('spam', 'spams.txt')
b = ('spam', 'spams', 'spams.ttt')
c = ('spam', 'spams', 'spams01.txt')
d = ('spam', 'spams', 'spams02.txt')
my_list = [a,b,c,d]
print sorted(my_list)
Output:
[('spam', 'spams', 'spams.ttt'), ('spam', 'spams', 'spams01.txt'), ('spam', 'spams', 'spams02.txt'), ('spam', 'spams.txt')]
Reply
#18
You are right, @buran. That is the current behaviour. Your example explains that very well.

I am talking about a would-be behaviour: that a list of pathlib.Paths and a list of string filename would behaved in the same manner. Of course it is just an opinion.

Perhaps the reason behind my opinion is that I regularly use pathlib.Path and string filenames as different implementations of the same notion: a path to a file or folder. Maybe I am disorientated on this point and should change my view.

In any case, I have submitted this issue as a possible bug for developers' consideration. I do not really think that it is a bug but... Just in case...

Thank you.
Reply
#19
it is clearly not a bug, but intended behavior. you can easily supply key argument  to sorted to get what you want

 
from pathlib import Path

filenames_for_testing = (
    '/spam/spams.txt',
    '/spam/spam.txt',
    '/spam/another.txt',
    '/spam/binary.bin',
    '/spam/spams/spam.ttt',
    '/spam/spams/spam01.txt',
    '/spam/spams/spam02.txt',
    '/spam/spams/spam03.ppp',
    '/spam/spams/spam04.doc',
)

paths_for_testing = [
    Path(filename)
    for filename in filenames_for_testing
]
sorted_paths = sorted(paths_for_testing, key=lambda p: p.as_posix())
print(sorted_paths)
Output:
[PosixPath('/spam/another.txt'), PosixPath('/spam/binary.bin'), PosixPath('/spam/spam.txt'), PosixPath('/spam/spams.txt'), PosixPath('/spam/spams/spam.ttt'), PosixPath('/spam/spams/spam01.txt'), PosixPath('/spam/spams/spam02.txt'), PosixPath('/spam/spams/spam03.ppp'), PosixPath('/spam/spams/spam04.doc')]
Reply
#20
Please look into the source code. Github makes it very easy for us.

The bult-in function sorted calls __lt__ (means less then) of an object.
So we should look into the pathlib module and seek for __lt__

It's hard to follow the code, because there is heavy inheritance and some other patterns.
You should know that pathobject._cparts returns a list spitted by it's parts. A path separator is also a part, if it's the root directory (absolute path).

Comparing lists in Python does not do what you think it should do. It compares in lexicographical order. I got this answer from here.

Comparing the paths as str:
'/spam/spams.txt' < '/spam/spams/spam04.doc'
Output:
True
Comparing the _cparts:
['/', 'spam', 'spams.txt'] < ['/', 'spam', 'spams', 'spam04.doc']
Output:
False
Index 0 and 1 are identical. Index 3 is bigger than index 3 from the other list. Then there are no more elements in the first list for comparison and the operation stops and yields the last result, which is False.
'spams.txt' < 'spam04.doc'
In simple words: Paths are sorted in alphabetical order by their elements.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Next/Prev file without loading all filenames WilliamKappler 9 514 Apr-12-2024, 05:13 AM
Last Post: Pedroski55
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 190 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  Sample random, unique string pairs from a list without repetitions walterwhite 1 459 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  trouble reading string/module from excel as a list popular_dog 0 428 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,175 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  String to List question help James_Thomas 6 987 Sep-06-2023, 02:32 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,546 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 922 Feb-23-2023, 02:21 PM
Last Post: sparkt
  convert string to float in list jacklee26 6 1,914 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  Checking if a string contains all or any elements of a list k1llcod3 1 1,108 Jan-29-2023, 04:34 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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