I have a text file that looks a bit like:
random text random text, can be anything blabla %A blabla
random text random text, can be anything blabla %D blabla
random text random text, can be anything blabla blabla %F
random text random text, can be anything blabla blabla
random text random text, %C can be anything blabla blabla
When I readlines()
it in, it becomes a list of sentences. Now I want this list to be sorted by the letter after the %
. So basically, when the sort is applied to the above, it should look like:
random text random text, can be anything blabla %A blabla
random text random text, %C can be anything blabla blabla
random text random text, can be anything blabla %D blabla
random text random text, can be anything blabla blabla %F
random text random text, can be anything blabla blabla
Is there a good way to do this, or will I have to break each string in to tubles, and then move the letters to a specific column, and then sort using key=operator.itemgetter(col)
?
Thank you
In [1]: def grp(pat, txt):
...: r = re.search(pat, txt)
...: return r.group(0) if r else '&'
In [2]: y
Out[2]:
['random text random text, can be anything blabla %A blabla',
'random text random text, can be anything blabla %D blabla',
'random text random text, can be anything blabla blabla %F',
'random text random text, can be anything blabla blabla',
'random text random text, %C can be anything blabla blabla']
In [3]: y.sort(key=lambda l: grp("%\w", l))
In [4]: y
Out[4]:
['random text random text, can be anything blabla %A blabla',
'random text random text, %C can be anything blabla blabla',
'random text random text, can be anything blabla %D blabla',
'random text random text, can be anything blabla blabla %F',
'random text random text, can be anything blabla blabla']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With