0

I have the following data in a text file.

[Test id_g001,**Test id_g002,Test id_g000, Value_is_0, Value_is_2, Value_is_1]

I can only sort the data. How do you sort the data in parallel, so the data will be sorted as follows? Both of the Test ID and Value needs to be sorted

 Test ID ---------------Value        
 g000 ------------------- 0  
 g001  ------------------- 1  
 g002  ------------------- 2

The code is:

def readFile():
from queue import PriorityQueue
q = PriorityQueue()
#try block will execute if the text file is found
try:
    fileName= open("textFile.txt",'r')
    for line in fileName:
            for string in line.strip().split(','):
                q.put(string[-4:])
    fileName.close() #close the file after reading          
    print("Displaying Sorted Data")
    while not q.empty():
        print(q.get())
        #catch block will execute if no text file is found
except IOError:
            print("Error: FileNotFoundException")
            return
Aurora_Titanium
  • 472
  • 1
  • 8
  • 23

1 Answers1

0

Let's say you have a list of strings as belows:

>>> l = ['Test id_g001','**Test id_g002','Test id_g000', 'Value_is_0', 'Value_is_2', 'Value_is_1']

And you want to sort them according to values of ID and Value, then you can do it this way:

>>> from operator import itemgetter
>>> sorted(l, key=itemgetter(-1))
['Test id_g000', 'Value_is_0', 'Test id_g001', 'Value_is_1', '**Test id_g002', 'Value_is_2']
Iron Fist
  • 10,739
  • 2
  • 18
  • 34