All the filenames are in the format ‘frame_xxx.jpg’, like
When I got all these filenames by os.listdir()
and put them in a list. Their order is uncorrect.
The code I got all the filenames are shown as following.
The list saved all the filenames are disordered.
Since they are frame names of a video, they should be processed in right order. So we have to sort the list before further processing.
The solution is using lambda expression to get the number of the framename and sort the list by the number. The code is shown as following.
There is no need to define a function. Supposing frames is the list you saved all the framenames, you can just use frames.sort(key=lambda i: int(re.findall(r'\d+', i)[0]))
to sort the list. And finally you will get all framenames in right order.
There are some useful blogs for you to get a clear of lambda expression and the sort strategy.
Python 列表内字符串按照规则排序(字符串列表的排序)
搞清楚 Python 正则表达式,简单易懂看这一篇就够了
python正则表达式从字符串中提取数字的思路详解
玩转python的正则表达式|提取字符串中的所有数字