3

I have been working on this program for a while and can't figure out how to sort one of my lists from the contents in the second list. For this program, I have a list of words and I also have a list of how many times the word is in the file I opened. I need to sort the word list according to the frequency of the word in descending order. I have to write a separate function to do this according to the assignment. I have a function that was given to the class to use but it only sorts one list. Here is the function I have and need to modify:

def selectionsort(mylist):
    for i in range(len(mylist)):
       max_i = i
       for j in range( i + 1, len(mylist) ):
           if mylist[j] > mylist[max_i]:
                max_i = j
       temp = mylist[max_i]
       mylist[max_i] = mylist[i]
       mylist[i] = temp

I currently have two lists that look like this:

mylist = ["the", "cat", "hat", "frog"]
frequency = [4, 1, 2 ,1]       

4 is the frequency of "the", 1 is the frequency of "cat" and so on.

My goal is to have mylist sorted like this:

 mylist = ["the", "hat", "cat", "frog"]

How should i modify the function i have so it sorts mylist using the corresponding values from the frequency list?

I am using Python 3.3

Kritzefitz
  • 2,644
  • 1
  • 20
  • 35
user3088605
  • 951
  • 1
  • 8
  • 5
  • Take 2 lists to start with. Whenever you swap, remember to swap the same positions in the two lists. Also, a pythonic approach would be to zip the lists, sort it with one of the elements, and get the other items as the result. – UltraInstinct Dec 11 '13 at 08:52

4 Answers4

3

Here you go! Using sorted and zip:

sortedlist = [i[0] for i in sorted(zip(mylist, frequency), key=lambda l: l[1], reverse=True)]

Here's a little demo:

>>> mylist
['the', 'cat', 'hat', 'frog']
>>> frequency = [4, 1, 3, 2]
>>> sortedlist = [i[0] for i in sorted(zip(mylist, frequency), key=lambda l: l[1], reverse=True)]
>>> sortedlist
['the', 'hat', 'frog', 'cat']

Hope this helps!

aIKid
  • 26,968
  • 4
  • 39
  • 65
  • 1
    Although correct, the OP would like to modify his code, not replace it entirely. Check my comment below the question. – UltraInstinct Dec 11 '13 at 08:54
  • @Thrustmaster */sigh*. You're right, but making it ourself is far easier than fixing others'. Hahaha. Hang on a second – aIKid Dec 11 '13 at 08:55
  • Exactly the reason why I commented. Rolling out our code is far easier, but it seldom helps someone who wants to learn & know whats wrong with his code :) – UltraInstinct Dec 11 '13 at 08:56
  • 2
    @aIKid This answer is wrong in the way that it is not what OP requested. – Saša Šijak Dec 11 '13 at 08:57
  • @SašaŠijak No. This is still a valid answer, since it solves his needs. – aIKid Dec 11 '13 at 08:59
  • @aIKid his question is "How should i modify the function i have so it sorts mylist using the corresponding values from the frequency list?" I do not see his function, modified, in your answer. Your answer would be really good, if he asked for alternative solution – Saša Šijak Dec 11 '13 at 09:01
  • I take the question from the title. You're free to downvote, however. – aIKid Dec 11 '13 at 09:03
  • @SašaŠijak Agreed 100%. Alternative answers, regardless how correct they are, should not make it to answers (*in my opinion*); a comment would do. But still, I think it still does not warrant a downvote. Downvote implies answer is plain wrong. :) – UltraInstinct Dec 11 '13 at 09:05
  • This is plain wrong. It is not actually sorting by the frequencies; it is sorting the words by reverse dictionary order. – user2357112 Dec 11 '13 at 09:20
  • @user2357112 Um... sorry? What do you mean? – aIKid Dec 11 '13 at 09:22
  • Compare your output to the output the OP wants. You will find that they do not match. – user2357112 Dec 11 '13 at 09:22
  • @user2357112 Ah, crap. I changed it to a wrong one. Rollback-ed. – aIKid Dec 11 '13 at 09:26
2
def selectionsort(mylist, frequences):
    for i in range(len(mylist)):
       max_i = i
       for j in range( i + 1, len(mylist) ):
           if frequences[j] > frequences[max_i]:
                max_i = j
       temp = mylist[max_i]
       mylist[max_i] = mylist[i]
       mylist[i] = temp
       temp = frequences[max_i]
       frequences[max_i] = frequences[i]
       frequences[i] = temp

You just have to incorporate frequences list in to the function and compare its values instead mylist values, and swap it`s values along the mylist values.

Saša Šijak
  • 8,717
  • 5
  • 47
  • 82
1
zip(*sorted(zip(frequency, mylist))[::-1])[1]
U2EF1
  • 12,907
  • 3
  • 35
  • 37
1
mylist = ["the", "cat", "hat", "frog"]
frequency = [4, 1, 2 ,1]
adict = dict(zip(mylist, frequency))
sorted(mylist, key = lambda x:adict[x], reverse = True)

a slightly different version. I believe it's more straightforward than the 1st answer, although more expensive (by making a new dict).

Twisted Meadow
  • 443
  • 2
  • 7