How to sort a list of strings on a substring

MRAB python at mrabarnett.plus.com
Mon Oct 5 19:05:09 EDT 2009


Scott wrote:
> I create a list of logs called LogList. Here is a sample:
> 
> LogList =
> ["inbound tcp office 192.168.0.125 inside 10.1.0.91 88",
> "inbound tcp office 192.168.0.220 inside 10.1.0.31 2967",
> "inbound udp lab 172.24.0.110 inside 10.1.0.6 161",
> "inbound udp office 192.168.0.220 inside 10.1.0.13 53"]
> 
> I want to sort the list on index 3 of each string - the first IP
> Address.
> 
> I only need strings with similar, first IP's to be together. I don't
> need all of the IP's to be in order. For example:
> either:
> SortedList =
> ["inbound udp lab 172.24.0.110 inside 10.1.0.6 161",
> "inbound tcp office 192.168.0.220 inside 10.1.0.31 2967",
> "inbound udp office 192.168.0.220 inside 10.1.0.13 53",
> "inbound tcp office 192.168.0.125 inside 10.1.0.91 88"]
> -or-
> SortedList =
> ["inbound tcp office 192.168.0.220 inside 10.1.0.31 2967",
> "inbound udp office 192.168.0.220 inside 10.1.0.13 53",
> "inbound udp lab 172.24.0.110 inside 10.1.0.6 161",
> "inbound tcp office 192.168.0.125 inside 10.1.0.91 88"]
> -or-
> etc.
> 
> would be fine.
> 
> I'm reading a lot on sort, sorted, cmp, etc. but I'm just not getting
> how to use an element of a string as a "key" within a list of strings.
> I'm using Python 2.6.2.
> 
Forget about cmp, just use the 'key' argument of the list's 'sort'
method or the 'sorted' function (the latter is better if you want to
keep the original list). The 'key' argument expects a function (anything
callable, actually) that accepts a single argument (the item) and
returns a value to be used as the key, and the items will be sorted
according to that key. In this case you want the items sorted by the
fourth 'word', so split the item into words and return the one at index
3:

def key_word(item):
     return item.split()[3]

SortedList = sorted(LogList, key=key_word)

If the function is short and simple enough, lambda is often used instead
of a named function:

SortedList = sorted(LogList, key=lambda item: item.split()[3])




More information about the Python-list mailing list