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:
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:
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
CodePudding user response:
You can use sorted
with a regex as custom key. Assuming l
the input list:
import re
sorted(l, key=lambda s: re.findall(r' 20\.\d ', s))
Alternatively, using the .
as delimiter to split
:
sorted(l, key=lambda s: s.split('.')[1])
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']
CodePudding user response:
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