Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

May 27, 2011

python 3.2 semi natural sorting


import re

pose_list = "Pose1", "Pose10", "Pose11", "Pose2", "Pose3", "Pose4"
pattern = '\d+' # one or more numerical characters

# this function assumes list consistency.
def MyFn(pose):
match = re.search(pattern, pose)
return int(match.group())

# this returns a sorted list given the presence of a numerical sequence in the string
print(sorted(pose_list, key=MyFn))

# sorted() now returns ['Pose1', 'Pose2', 'Pose3', 'Pose4', 'Pose10', 'Pose11']

(unfortunately) i think blender will automatically resort that list alphabetically it is advised to add padding zeroes.