How i can use python to sort the list format
format=[\"12 sheet\",\"4 sheet\",\"48 sheet\",\"6 sheet\", \"busrear\", \"phonebox\",\"train\"]
<
You can trivially sort a list of strings with built-in sorted
method. Even if the objects in your list are more complex, you can still use sorted
. Just pass a custom key parameter to use the second item from the inner list as the key in ordering comparisons:
result = sorted(format, key=lambda x: x[1])
Finally switch to your sorting function natsorted (from natsort package) and you end up with the desired, naturally sorted, result list:
from natsort import natsorted
result = natsorted(format, key=lambda x: x[1])