根据项目内的特定字符串对 python 列表的项目进行排序

sort items of a python list, based on specific string inside item

我有以下列表:

['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']

当用 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

我如何根据“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

您可以将 sorted 与正则表达式一起用作自定义键。假设 l 输入列表:

import re
sorted(l, key=lambda s: re.findall(r' 20\.\d+', s))

或者,使用 . 作为 split 的分隔符:

sorted(l, key=lambda s: s.split('.')[1])

输出:

['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