You seem to be mixing two problems here. First is sorting a nested list like that, and the second is how to sort according to those RZ
things.
You can do the former by transposing the multidimensional array first so that items that belong together are actually in the same sublist. And then you can just sort according to your sort function on the 3rd list item.
>>> list(zip(*sorted(zip(*lst), key=lambda x: x[2])))
[('3', '1', '2', '4'), ('some text 3', 'some text 1', 'some text 2', 'some text 4'), ('some RZ-text to analyze', 'some text', 'some text to analyze with RZstring', 'some text to analyze with no rz-string and no textRZ')]
For your second problem however, I don’t really understand what this sorting is based on. If it was the prefix before RZ
, then the latter two items would still be reversed, or not?
Based on your updated specification in the comments, you could use a regular expression to check if RZ
occurs with a word boundary in front (\b
) and add that fact in from of the sort-key:
>>> import re
>>> list(zip(*sorted(zip(*lst), key=lambda x: (re.search(r'\bRZ', x[2]) != None, x[2]))))
[('1', '4', '3', '2'), ('some text 1', 'some text 4', 'some text 3', 'some text 2'), ('some text', 'some text to analyze with no rz-string and no textRZ', 'some RZ-text to analyze', 'some text to analyze with RZstring')]