0

I have a list named

l=[('Altamont','TN'),('Baxter','TN'),('Norway','SC'),('Trenton','SC')]

initially, which is in the order of ('cities', 'states')

After sorting based on states and cities, and writing to CSV by below the functions,

def calcDistance(self):
 filePath = self.dlg.fileNameEdit.text()
 l=[('Altamont','TN'),('Baxter','TN'),('Norway','SC'),('Trenton','SC')]
 sorted(l, key=operator.itemgetter(1,0))
self.writeToFile(l, filePath)

def writeToFile(self, l, filePath):
    fileCSV = open(filePath, "w")

    for i in l:
          fileCSV.write(i)
          fileCSV.write(",")
          fileCSV.write("\n")
          QMessageBox.information(self.dlg, "Number of Cities Near", str(len(lsOfCities)) + " citie(s) are written to "+filePath)
          fileCSV.close()
          self.dlg.close()

After it runs, I will be getting a CSV file as below:

'Baxter','TN'
'Altamont','TN'
'Trenton','SC'
'Norway','SC'

I want to know that whether there is any possible way in Python2.7, if I do sorting on the second column (i.e states) of the CSV, the cells corresponding to that should also be sorted?

like

'Norway','SC'
'Trenton','SC'
'Altamont','TN'
'Baxter','TN'
harinish
  • 261
  • 3
  • 5
  • 17

2 Answers2

2

If you want to sort by state name, then by city name, the flowing code :

sorted(l, key=operator.itemgetter(1,0))

doesn't make any sense, because here l is still immutable. You should re-assign:

l = sorted(l, key=operator.itemgetter(1,0))

After this sorting ,I tried in my computer:

>>> l = [('Altamont','TN'),('Baxter','TN'),('Norway','SC'),('Trenton','SC')]
>>> import operator
>>> l = sorted(l, key = operator.itemgetter(1,0))
>>> l
[('Norway', 'SC'), ('Trenton', 'SC'), ('Altamont', 'TN'), ('Baxter', 'TN')]
chenzhongpu
  • 6,193
  • 8
  • 41
  • 79
  • Well your code is fine if i want to sort by city name, then by state name. But i need to sort by state first and then by city.And please make note that my list in the form of (cities, state). Please can you edit your answer for it?I am new to python. – harinish Apr 20 '15 at 05:44
  • `(1, 0)` means sorted by second item, then first item, so I have done sorted by 'state' first, and please refer to http://stackoverflow.com/questions/4233476/sort-a-list-by-multiple-attributes – chenzhongpu Apr 20 '15 at 05:51
  • Yeah i agree its state first and cities second. my final output awrites as shown below Ballentine SC Bamberg SC Barnwell SC Batesburg SC Bath SC Baxter TN Baker Hill AL Bangor AL Banks AL – harinish Apr 20 '15 at 06:05
  • But i need is to appear like this Baker Hill AL Bangor AL Banks AL Ballentine SC Bamberg SC Barnwell SC Batesburg SC Bath SC Baxter TN. I want the first alphabet of the state column along with its corresponding cell to be my first cell and so on. – harinish Apr 20 '15 at 06:07
  • @harinish: When I copy the exact list of cities and states from your comment, `random.shuffle` it, then use the exact code from this answer, I get the exact result you want. Just as you should expect. You're clearly not doing the same thing the answer suggests. – abarnert Apr 20 '15 at 06:14
0

Your problem is that you are sorting the list and not assigning it to anything.

def calcDistance(self):
  filePath = self.dlg.fileNameEdit.text()
  l = [('Altamont','TN'),('Baxter','TN'),('Norway','SC'),('Trenton','SC')]
  l = sorted(l, key=operator.itemgetter(1,0))

See example below:

>>> a = [4,3,2]
>>> sorted(a)
[2, 3, 4]
>>> a
[4, 3, 2]
>>>
dting
  • 38,604
  • 10
  • 95
  • 114