Sep-03-2018, 05:27 PM
(Sep-03-2018, 01:35 PM)ujjwalrathod007 Wrote: also the following code is not assigning images in ascending orderIt can be wrong if just sort the strings,it's called natural sorting(human sorting).
Order of images is important for input to my Algorithm,
So i guess you want to sort after floating value in string.
import re from pprint import pprint def try_float(text): try: return float(text) except ValueError: text def natural(text): return [try_float(c) for c in re.split('(\d+.\d+)', text)] lst = [ '1305031102.975304.png', '1305031102.211214.png', '1305031102.243211.png', '1305031102.275326.png', '1305031102.311267.png', '1305031102.343233.png', '1305031102.375329.png', '1305031102.411258.png', '1305031102.443271.png', ] lst.sort(key=natural) pprint(lst)
Output:['1305031102.211214.png',
'1305031102.243211.png',
'1305031102.275326.png',
'1305031102.311267.png',
'1305031102.343233.png',
'1305031102.375329.png',
'1305031102.411258.png',
'1305031102.443271.png',
'1305031102.975304.png']
See that i changed '1305031102.9
75304.png'Now it's largest float value an are last is list.