例如,有没有办法对以下内容进行排序:
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
仅使用“模型”之后的整数?我试图使用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是最大的。
答案 0 :(得分:3)
使用正则表达式的版本:
rgx = re.compile('model_(?P<number>\d+)')
srtd = sorted(mods, key=lambda x: int(rgx.match(x).group('number')))
正则表达式model_(?P<number>\d+)
在字符串\d+
之后捕获数字model_
(贪婪)。在key
of sorted
中,然后将其转换为int
。
我假设您的输入位于名为mods
的列表中:
mods = [
'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',
...
]
如果它只是你提出的字符串,你就会以某种方式将它带入这种形式。
稍微冗长(没有命名组)可以像这样实现:
rgx = re.compile('model_(\d+)')
srtd = sorted(mods, key=lambda x: int(rgx.match(x).group(1)))
答案 1 :(得分:2)
鉴于此输入:
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'''
您可以执行以下操作:
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
但正如我在评论中所说,正则表达式解决方案在这里感觉更合适。