[英]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
.我试图获取目录中所有文件的顺序,所以我尝试使用 sorted() function 对分别命名为folder/frame0.jpg
的每个文件的数值进行排序。 The problem is I am getting an output that looks like this问题是我得到一个看起来像这样的 output
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?有人知道我应该怎么做吗?
Specify a key
parameter to sorted
to tell it how to sort the strings that's different from standard alphabetical comparison.指定一个要sorted
的key
参数,告诉它如何对不同于标准字母比较的字符串进行排序。
>>> 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 (ie 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
.如果您的真实数据集更复杂(即前导非数字部分是可变的,并且您想对其进行排序加上数值),则您的key
function 可能会执行类似返回前导str
和尾随int
的元组的操作.
You need to index the filenames by the number in the filename (by first extracting the number in the first place), and then zero-pad the number, and sort the index.您需要通过文件名中的数字对文件名进行索引(首先提取数字),然后对数字进行零填充,并对索引进行排序。 Like this:像这样:
import os
def index_frames(folder):
indexed_files = {}
for filename in os.listdir(folder):
path = (os.path.join(folder, filename))
if path.endswith('jpg'):
# extract the number from the filename and pad with zeros so that they will sort correctly
num = ''.join([s for s in filename if s.isdigit()]).zfill(15)
# insert into a dict with key as padded number
indexed_files[num] = path
return indexed_files
index_frames = index_frames('yourlieinapril1')
for key in sorted(index_frames.keys()):
# these will print in order because they have been sorted by the index
print(index_frames[key])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.