user5473706
user5473706

Reputation: 29

Organizing Sorts

I have this list called countries.txt that list all the countries by their name, area(in km2), population (eg. ["Afghanistan",647500.0,25500100]).

def readCountries(filename):

    result=[]
    lines=open(filename)

    for line in lines:
        result.append(line.strip('\n').split(',\t'))
    for sublist in result:
        sublist[1]=float(sublist[1])

I also have two sorting algorithms:

def countryByArea(area):
    myList=readCountries('countries.txt') 

    for i in range(0,len(myList)): 
        for j in range(0,len(myList)-1): 
            if myList[j][1]>myList[j+1]:
                temp=myList[j]   
                myList[j]=myList[j+1]
                myList[j+1]=temp

    if area < len(myList): 
        return myList[area-1] 
    else:
        print "Invalid Parameter: %s" % (area) 

AND

myList=readCountries('countries.txt')

for i in range(0,len(myList)):  
    for j in range(0,len(myList)-1): 
        if myList[j][1]>myList[j+1]: 
            temp=myList[j]  
            myList[j]=myList[j+1]
            myList[j+1]=temp
if area < len(myList): 
    return myList[area-1] 
else:
    print "Invalid Parameter: %s" % (area)

I am trying to determine which countries are in the same order for both population and area. If a country is both the 7th most populated and 7th largest by area, it should be reported by your function along with its rank (i.e., 7th).

I was thinking you could copy the two sorts returning the whole organized list and then make a for loop that compares the elements in each of those organized lists and if they are the same print them

Upvotes: 0

Views: 62

Answers (1)

eguaio
eguaio

Reputation: 3954

I think your idea of comparing the two sorted list is correct. I think you have a bug in line myList[j][1]>myList[j+1]:, since there you are comparing an item of the list with an attribute of another item.

As far as I can see, you implemented the bubble sorting algorithm. You should not implement your own sorting algorithms unless you have some extra information about the data that tells you this is the best choice. And even in that case, it is always better to use some tested and largely used library. For example, in your algorithm, it is not necessary that both variables iterate from 0 to n.

One observation is that you should not be reading twice the file, you can read it once and then copy the contents. Besides, since you are using lists of attributes as the items of the main list (which are mutable), when you copy the main list with the list() constructor, only the references of the items are copied leading to a more efficient use of the memory (also, take into account this if you want to modify the items in only one list). In our case, we only work on the order, that does not chance the elements.By the way, I really recommend you to read pep-8.

Anyway, you could achieve what you want as follows:

by_name_list = read_countries('countries.txt')
by_area_list = list(by_name_list) # a shallow copy of the list
by_name_list.sort(key=lambda i: i[0]) # sort by name
by_area_list.sort(key=lambda i: i[1]) # sort by area

for k in xrange(len(by_name_list)):
    country_name = by_name_list[k][0]
    if by_area_list[k][0] == country_name:
        print 'Same order: %s in position %d'%(country_name, k)

Upvotes: 1

Related Questions