[英]sort items of a python list, based on specific string inside item
I have the following list:我有以下列表:
['2022-04-07 16:42:26,469 20.1.Starting all probes', '2022-04-07 16:42:26,469 20.1.: Pinging 192.168.255.68 started', '2022-04-07 16:42:26,472 20.2.Username, password not present, starting probes', '2022-04-07 16:42:26,472 20.2.: Pinging 192.168.254.108 started', '2022-04-07 16:42:29,817 20.1.: Pinging 192.168.255.68 succeeded', '2022-04-07 16:42:29,819 20.1.: SSH log for 192.168.255.68', '2022-04-07 16:42:29,820 20.2.: Pinging 192.168.254.108 succeeded', '2022-04-07 16:42:29,822 20.2.All probes finished', '2022-04-07 16:42:34,938 20.1.: Successfully connected to 192.168.255.68', '2022-04-07 16:42:35,629 20.1.: Interactive SSH session established', '2022-04-07 16:42:35,629 20.1.All probes finished']
When displaying it with for loop I'll get:当用 for 循环显示它时,我会得到:
2022-04-07 16:42:26,469 20.1.Starting all probes
2022-04-07 16:42:26,469 20.1.: Pinging 192.168.255.68 started
2022-04-07 16:42:26,472 20.2.Username, password not present, starting probes
2022-04-07 16:42:26,472 20.2.: Pinging 192.168.254.108 started
2022-04-07 16:42:29,817 20.1.: Pinging 192.168.255.68 succeeded
2022-04-07 16:42:29,819 20.1.: SSH log for 192.168.255.68
2022-04-07 16:42:29,820 20.2.: Pinging 192.168.254.108 succeeded
2022-04-07 16:42:29,822 20.2.All probes finished
2022-04-07 16:42:34,938 20.1.: Successfully connected to 192.168.255.68
2022-04-07 16:42:35,629 20.1.: Interactive SSH session established
2022-04-07 16:42:35,629 20.1.All probes finished
How can I order it based on the number that follows "20", using sort() or something else to have it sorted based that number and then the date like usual:我如何根据“20”后面的数字对其进行排序,使用 sort() 或其他方法让它根据该数字进行排序,然后像往常一样根据日期进行排序:
2022-04-07 16:42:26,469 20.1.Starting all probes
2022-04-07 16:42:26,469 20.1.: Pinging 192.168.255.68 started
2022-04-07 16:42:29,817 20.1.: Pinging 192.168.255.68 succeeded
2022-04-07 16:42:29,819 20.1.: SSH log for 192.168.255.68
2022-04-07 16:42:34,938 20.1.: Successfully connected to 192.168.255.68
2022-04-07 16:42:35,629 20.1.: Interactive SSH session established
2022-04-07 16:42:35,629 20.1.All probes finished
2022-04-07 16:42:26,472 20.2.Username, password not present, starting probes
2022-04-07 16:42:26,472 20.2.: Pinging 192.168.254.108 started
2022-04-07 16:42:29,820 20.2.: Pinging 192.168.254.108 succeeded
2022-04-07 16:42:29,822 20.2.All probes finished
You can use sorted
with a regex as custom key.您可以使用
sorted
和正则表达式作为自定义键。 Assuming l
the input list:假设
l
输入列表:
import re
sorted(l, key=lambda s: re.findall(r' 20\.\d+', s))
Alternatively, using the .
或者,使用
.
as delimiter to split
:作为
split
的分隔符:
sorted(l, key=lambda s: s.split('.')[1])
Output: Output:
['2022-04-07 16:42:26,469 20.1.Starting all probes',
'2022-04-07 16:42:26,469 20.1.: Pinging 192.168.255.68 started',
'2022-04-07 16:42:29,817 20.1.: Pinging 192.168.255.68 succeeded',
'2022-04-07 16:42:29,819 20.1.: SSH log for 192.168.255.68',
'2022-04-07 16:42:34,938 20.1.: Successfully connected to 192.168.255.68',
'2022-04-07 16:42:35,629 20.1.: Interactive SSH session established',
'2022-04-07 16:42:35,629 20.1.All probes finished',
'2022-04-07 16:42:26,472 20.2.Username, password not present, starting probes',
'2022-04-07 16:42:26,472 20.2.: Pinging 192.168.254.108 started',
'2022-04-07 16:42:29,820 20.2.: Pinging 192.168.254.108 succeeded',
'2022-04-07 16:42:29,822 20.2.All probes finished']
import re
def sort_list(lst):
lst_n = []
for text in lst:
n = re.search('\d{3} \d{2}.(.+?).*', text).group(1)
lst_n.append((text, n))
text_n_sorted = sorted(lst_n, key=lambda x: x[1])
sorted_text = []
for text, n in text_n_sorted:
sorted_text.append(text)
return sorted_text
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.