python - Can lambda be used instead of a method, as a key in sorted()? -


this first time asking question. i've got without asking. (gratitude face). so, found useful piece of code around here few weeks ago.

import re  def yearly_sort(value):     numbers = re.compile(r'(\d+)')     parts = numbers.split(value)     parts[1::2] = map(int, parts[1::2])     return parts 

it works here:

def get_files_sorted(path_to_dir):    file_names = []    root, dirs, files in os.walk(path_to_dir):       file_name in sorted(files, key=yearly_sort):          file_names.append(os.path.join(root, file_name))    return file_names 

now, i'm facing problem describing above function (as can see non-descriptive method name).

what should name of above method (instead of some_sort())?

can method squeezed somehow in lambda such key in sorted() can bind key=lambda?

you squeeze lambda provided numbers compiled (which increases performance)

numbers = re.compile(r'(\d+)') file_name in sorted(files, key=lambda value : [int(x) if x.isdigit() else 0 x in numbers.split(value)][1::2]): 

in case, unnecessary test items #0 , #3 onwards. maybe better, that's risk complex lambdas: performance & lisibility can suffer.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -