#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Sorted() not working correctly


    This is my code.

    Code:
    def unique_counts(words):
      # +++your code here+++
      counts = dict()
      for word in words:
            counts[word] = counts.get(word, 0) +1
    
      lst  = list()
    
      for word,count in counts.items():
              #Reverse the order of count and name so that later the list will be sorted
              #based on the count. 
              lst.append((word, count))
              
      lst.sort(reverse = True)
      
      return lst
    I'm suppose to

    return a list of tuples containing the counts of each of the strings. The returned results should be ordered by the counts in decreasing order. In case of ties of counts, break the tie by string value in increasing order


    My current output is this:

    OK got: [] expected: []
    OK got: [('Jasmine', 1)] expected: [('Jasmine', 1)]
    OK got: [('Jasmine', 2)] expected: [('Jasmine', 2)]
    OK got: [('Jasmine', 3), ('Belle', 2), ('Aurora', 1)] expected: [('Jasmine', 3), ('Belle', 2), ('Aurora', 1)]
    X got: [('Jasmine', 1), ('Irene', 1), ('Belle', 3), ('Aurora', 2), ('Adella', 1)] expected: [('Belle', 3), ('Aurora', 2), ('Adella', 1), ('Irene', 1), ('Jasmine', 1)]

    The green does not match the red. I can't figure out what is wrong with my sorted code to get it to produce the output like the red.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    475
    Rep Power
    33
    It works for me as expected
    Code:
    x=[('Jasmine', 1), ('Irene', 1), ('Belle', 3), ('Aurora', 2), ('Adella', 1)] 
    x.sort(reverse=True)
    print x
    
    """   results of print
    [('Jasmine', 1), ('Irene', 1), ('Belle', 3), ('Aurora', 2), ('Adella', 1)]
    """
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    I'm suppose to get this output:
    [('Belle', 3), ('Aurora', 2), ('Adella', 1), ('Irene', 1), ('Jasmine', 1)]

    not
    [('Jasmine', 1), ('Irene', 1), ('Belle', 3), ('Aurora', 2), ('Adella', 1)]

    The weird thing is that it seems to sort the previous line correctly, but not the very last line.

    Sorry, I'm still very new to python.


    EDIT:

    It's almost there!

    I played around with the code and now I only need to order it by alphabetical/increasing order for the last line.

    So now I have

    [('Belle', 3), ('Aurora', 2), ('Jasmine', 1), ('Irene', 1), ('Adella', 1)]

    I just need to switch the last three names to get the output below.

    [('Belle', 3), ('Aurora', 2), ('Adella', 1), ('Irene', 1), ('Jasmine', 1)]
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    I haven't used tuples in the sorted() function before but can someone explain key=lambda tup: tup[#] a little bit to me?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    430
    Rep Power
    66
    Originally Posted by psk102000
    I haven't used tuples in the sorted() function before but can someone explain key=lambda tup: tup[#] a little bit to me?
    “key” argument is used to produce a sort key from the actual sequence element. The actual element is passed as the parameter. For example in “key=lambda tup: tup[1]” the actual element is given the name “tup”, and the key used in sorting is the second element of it – which implies that “tup” is a sequence that can be sliced; otherwise a TypeError is raised.

    Nameless lambda functions can be used for the simple cases. Anything more complex, you need to call an outside function, as in: “key=somefunction(this_element)”.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    606
    Rep Power
    65
    You need to do two sorts in sequence:
    Code:
    def by_frequency(tup):
        '''sort helper function, sort by second element in tuple'''
        return tup[1] 
    
    q = [('Jasmine', 1), ('Irene', 1), ('Belle', 3), ('Aurora', 2), ('Adella', 1)]
    # sort by name first (default)
    q.sort()
    # then by frequency reversed
    print(sorted(q, key=by_frequency, reverse=True))
    
    ''' result -->
    [('Belle', 3), ('Aurora', 2), ('Adella', 1), ('Irene', 1), ('Jasmine', 1)]
    '''
    Last edited by Dietrich; January 22nd, 2013 at 04:34 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Actually, you don't need to do two sorts. Since Python compares tuples by looking at the first element first, then the second if the first elements compare equal, you can simply make a key function returning a tuple:

    Code:
    def _my_key_func(tup):
        #Take the negative of frequency so it sorts in reverse.
        return (-tup[1], tup[0])
    And testing it out:
    Code:
    sorted([('Jasmine', 1), ('Irene', 1), ('Belle', 3), ('Aurora', 2),
            ('Adella', 1)], key=_my_key_func)
    [('Belle', 3), ('Aurora', 2), ('Adella', 1), ('Irene', 1), ('Jasmine', 1)]

    Comments on this post

    • Dietrich agrees : nice code

IMN logo majestic logo threadwatch logo seochat tools logo