class SortNum(object):
def __init__(self, lst):
self.lst = lst
@staticmethod
def convert2int(s):
try:
return int(s)
except ValueError:
return s
def str2int(self, v_str):
return [self.convert2int(sub_str) for sub_str in re.split('([0-9]+)', v_str)]
def sort_num(self):
return sorted(self.lst, key=self.str2int)
def main():
test = ['0', '12', '2']
print(sorted(test)) # ['0', '12', '2']
print(SortNum(test).sort_num()) # ['0', '2', '12']
如何用python将列表中的字符串元素按数字大小排序?
最新推荐文章于 2024-04-30 15:36:09 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)