Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
78 views
in Technique[技术] by (71.8m points)

python - sorted() function not working on numeric list

I am trying to get the order of all files in the directory so I tried using sorted() function to sort the numeric value of each file which are respectively named folder/frame0.jpg. The problem is I am getting an output that looks like this

yourlieinapril1/frame1094.jpg
yourlieinapril1/frame1095.jpg
yourlieinapril1/frame1096.jpg
yourlieinapril1/frame1097.jpg
yourlieinapril1/frame1098.jpg
yourlieinapril1/frame1099.jpg
yourlieinapril1/frame1100.jpg
yourlieinapril1/frame110.jpg
yourlieinapril1/frame1101.jpg

when ever it iterates through a base ten number the list goes back to a lesser number which I tried fixing with the following code but it still doesn't work well

def animate(folder):
    l = list()
    a = list()
    for filename in os.listdir(folder):
        path = (os.path.join(folder, filename))
        if path.endswith('jpg'):
            num = ''
            for i in range(len(path)):
                try:
                    p = int(path[i])
                    num += str(path[i])
                except:
                    pass
            try:
                check = a[len(a) - 1]
                if len(check) > len(num):
                    if num != '0':
                        num += ('0')
                        path = folder + '/frame' + num + '.jpg'
            except:
                pass
            a.append(num)
            l.append(path)
    f = sorted(l)
    f = list(dict.fromkeys(f))

does anyone have an idea of how I should do this?

question from:https://stackoverflow.com/questions/65888040/sorted-function-not-working-on-numeric-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Specify a key parameter to sorted to tell it how to sort the strings that's different from standard alphabetical comparison.

>>> files = [
...     "yourlieinapril1/frame1094.jpg",
...     "yourlieinapril1/frame1095.jpg",
...     "yourlieinapril1/frame1096.jpg",
...     "yourlieinapril1/frame1097.jpg",
...     "yourlieinapril1/frame1098.jpg",
...     "yourlieinapril1/frame1099.jpg",
...     "yourlieinapril1/frame1100.jpg",
...     "yourlieinapril1/frame110.jpg",
...     "yourlieinapril1/frame1101.jpg",
... ]
>>> sorted(files, key=lambda s: int(s[21:-4]))
['yourlieinapril1/frame110.jpg', 'yourlieinapril1/frame1094.jpg', 'yourlieinapril1/frame1095.jpg', 'yourlieinapril1/frame1096.jpg', 'yourlieinapril1/frame1097.jpg', 'yourlieinapril1/frame1098.jpg', 'yourlieinapril1/frame1099.jpg', 'yourlieinapril1/frame1100.jpg', 'yourlieinapril1/frame1101.jpg']

If your real data set is more complicated (i.e. the leading non-numeric part is variable and you want to sort on that plus the numeric value), your key function might instead do something like returning a tuple of the leading str and the trailing int.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...