Yes, you can. You can sort the last 3 digits in each test substring:
# The string to be sorted by digits s = "Test DATA_g004, Test DATA_g003, Test DATA_g001, Test DATA_g002"
You want to trim the l elements if this is important to you, but this will work for all Test that end in 3 digits.
If you do not want Test DATA_ , you can do this:
# The string to be sorted by digits s = "Test DATA_g004, Test DATA_g003, Test DATA_g001, Test DATA_g002"
If your data is well formed (i.e. g and then 3 digits), this will work very well. Otherwise, use the regex from other posted answers.
Another alternative is to insert rows in the PriorityQueue as they read:
test.py
from Queue import PriorityQueue q = PriorityQueue() with open("example.txt") as f: # For each line in the file for line in f: # Create a list from the stripped, split-at-comma string for s in line.strip().split(','): # Push the last four characters of each element in the list into the pq q.put(s[-4:]) while not q.empty(): print q.get()
The advantage of using PQ is that it will add them in a sorted order that removes the burden from you, and this is done in linear time.
example.txt
Test DATA_g004, Test DATA_g003, Test DATA_g001, Test DATA_g002
And the conclusion:
13:25 $ python test.py g001 g002 g003 g004