Python:有没有办法只使用字符串中选定的字符对字符串进行排序?

Python:有没有办法只使用字符串中选定的字符对字符串进行排序?,python,string,sorting,Python,String,Sorting,例如,是否有方法对以下内容进行排序: model_1_depth/depth_w/read/_79__cf__79 model_2_depth/depth_w/read/_73__cf__73 model_3_depth/depth_w/read/_67__cf__67 model_4_depth/depth_w/read/_61__cf__61 model_5_depth/depth_w/read/_55__cf__55 model_6_depth/depth_w/read/_49__cf__

例如,是否有方法对以下内容进行排序:

model_1_depth/depth_w/read/_79__cf__79
model_2_depth/depth_w/read/_73__cf__73
model_3_depth/depth_w/read/_67__cf__67
model_4_depth/depth_w/read/_61__cf__61
model_5_depth/depth_w/read/_55__cf__55
model_6_depth/depth_w/read/_49__cf__49
model_7_depth/depth_w/read/_43__cf__43
model_8_depth/depth_w/read/_37__cf__37
model_9_depth/depth_w/read/_31__cf__31
model_10_depth/depth_w/read/_25__cf__25
model_11_depth/depth_w/read/_19__cf__19
model_12_depth/depth_w/read/_13__cf__13
model_13_depth/depth_w/read/_7__cf__7
model_13_point/weights/read/_4__cf__4
model_12_point/weights/read/_10__cf__10
model_11_point/weights/read/_16__cf__16
model_10_point/weights/read/_22__cf__22
model_9_point/weights/read/_28__cf__28
model_8_point/weights/read/_34__cf__34
model_7_point/weights/read/_40__cf__40
model_6_point/weights/read/_46__cf__46
model_5_point/weights/read/_52__cf__52
model_4_point/weights/read/_58__cf__58
model_3_point/weights/read/_64__cf__64
model_2_point/weights/read/_70__cf__70
model_1_point/weights/read/_76__cf__76
model_0/weights/read/_82__cf__82
my_text = my_text.split('\n')

b = sorted(my_text, key=lambda x: int(x.split('/')[0].split('_')[1]))
my_text_out = '\n'.join(b)
print(my_text_out)
仅使用“model”后面的整数?我曾尝试使用
re.sub(““,”,re.sub(r'[^\w],'',key[6:8])
仅捕获这些整数,以获得要排序的整数,但我无法使用排序结果找到原始字符串,因为会有一些重复的字符串,“1”既属于
model_1_point/weights/read/_76_cf_76
又属于
model_1_depth/depth w/read/_79_cf_79
,无法区分两者

有没有一种优雅的方法可以做到这一点

编辑:为了澄清,以下字符串在字典中,我想排序为1最小,13最大。

给定此输入:

my_text = '''model_1_depth/depth_w/read/_79__cf__79
model_2_depth/depth_w/read/_73__cf__73
model_3_depth/depth_w/read/_67__cf__67
model_4_depth/depth_w/read/_61__cf__61
model_5_depth/depth_w/read/_55__cf__55
model_6_depth/depth_w/read/_49__cf__49
model_7_depth/depth_w/read/_43__cf__43
model_8_depth/depth_w/read/_37__cf__37
model_9_depth/depth_w/read/_31__cf__31
model_10_depth/depth_w/read/_25__cf__25
model_11_depth/depth_w/read/_19__cf__19
model_12_depth/depth_w/read/_13__cf__13
model_13_depth/depth_w/read/_7__cf__7
model_13_point/weights/read/_4__cf__4
model_12_point/weights/read/_10__cf__10
model_11_point/weights/read/_16__cf__16
model_10_point/weights/read/_22__cf__22
model_9_point/weights/read/_28__cf__28
model_8_point/weights/read/_34__cf__34
model_7_point/weights/read/_40__cf__40
model_6_point/weights/read/_46__cf__46
model_5_point/weights/read/_52__cf__52
model_4_point/weights/read/_58__cf__58
model_3_point/weights/read/_64__cf__64
model_2_point/weights/read/_70__cf__70
model_1_point/weights/read/_76__cf__76
model_0/weights/read/_82__cf__82'''
您可以执行以下操作:

model_1_depth/depth_w/read/_79__cf__79
model_2_depth/depth_w/read/_73__cf__73
model_3_depth/depth_w/read/_67__cf__67
model_4_depth/depth_w/read/_61__cf__61
model_5_depth/depth_w/read/_55__cf__55
model_6_depth/depth_w/read/_49__cf__49
model_7_depth/depth_w/read/_43__cf__43
model_8_depth/depth_w/read/_37__cf__37
model_9_depth/depth_w/read/_31__cf__31
model_10_depth/depth_w/read/_25__cf__25
model_11_depth/depth_w/read/_19__cf__19
model_12_depth/depth_w/read/_13__cf__13
model_13_depth/depth_w/read/_7__cf__7
model_13_point/weights/read/_4__cf__4
model_12_point/weights/read/_10__cf__10
model_11_point/weights/read/_16__cf__16
model_10_point/weights/read/_22__cf__22
model_9_point/weights/read/_28__cf__28
model_8_point/weights/read/_34__cf__34
model_7_point/weights/read/_40__cf__40
model_6_point/weights/read/_46__cf__46
model_5_point/weights/read/_52__cf__52
model_4_point/weights/read/_58__cf__58
model_3_point/weights/read/_64__cf__64
model_2_point/weights/read/_70__cf__70
model_1_point/weights/read/_76__cf__76
model_0/weights/read/_82__cf__82
my_text = my_text.split('\n')

b = sorted(my_text, key=lambda x: int(x.split('/')[0].split('_')[1]))
my_text_out = '\n'.join(b)
print(my_text_out)
其结果是:

model_0/weights/read/_82__cf__82
model_1_depth/depth_w/read/_79__cf__79
model_1_point/weights/read/_76__cf__76
model_2_depth/depth_w/read/_73__cf__73
model_2_point/weights/read/_70__cf__70
model_3_depth/depth_w/read/_67__cf__67
model_3_point/weights/read/_64__cf__64
model_4_depth/depth_w/read/_61__cf__61
model_4_point/weights/read/_58__cf__58
model_5_depth/depth_w/read/_55__cf__55
model_5_point/weights/read/_52__cf__52
model_6_depth/depth_w/read/_49__cf__49
model_6_point/weights/read/_46__cf__46
model_7_depth/depth_w/read/_43__cf__43
model_7_point/weights/read/_40__cf__40
model_8_depth/depth_w/read/_37__cf__37
model_8_point/weights/read/_34__cf__34
model_9_depth/depth_w/read/_31__cf__31
model_9_point/weights/read/_28__cf__28
model_10_depth/depth_w/read/_25__cf__25
model_10_point/weights/read/_22__cf__22
model_11_depth/depth_w/read/_19__cf__19
model_11_point/weights/read/_16__cf__16
model_12_depth/depth_w/read/_13__cf__13
model_12_point/weights/read/_10__cf__10
model_13_depth/depth_w/read/_7__cf__7
model_13_point/weights/read/_4__cf__4
但正如我在评论中所说的那样,正则表达式解决方案在这里更合适。

使用正则表达式的版本:

rgx = re.compile('model_(?P<number>\d+)')
srtd = sorted(mods, key=lambda x: int(rgx.match(x).group('number')))
如果它只是你展示的字符串,你会以某种方式将它带入这种形式


稍微少一点冗长(没有命名的组),同样可以通过以下方式实现:

rgx = re.compile('model_(\d+)')
srtd = sorted(mods, key=lambda x: int(rgx.match(x).group(1)))

这些字符串是在
列表中还是什么的?如果你遇到的问题是那不是一个列表,你可以通过
你的字符串.split()
b=sorted(a,key=lambda x:int(x.split('/')[0]。split('/')[1])
把它变成一个,但我更喜欢
re
gex解决方案。谢谢你的回答,它也很管用!我相信正则表达式的解决方案是这样的:
排序(dictionary,key=lambda key:int(re.sub(““,”,re.sub(r'[^\w],'',key[6:8]))
,我在前面添加了一个int(不知道他的答案去了哪里),对它做了一些修改。谢谢大家的快速回复。