#16
  1. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,281
    Rep Power
    13
    for key in sorted( months.keys() ):
    for key in sorted( months.values() ):


    fail. meybe this is abug andsorted function doesnt work as expected.
    What is now proved was once only imagined!
  2. #17
  3. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,281
    Rep Power
    13
    After many tried this did the job:

    for key in sorted(months.items(),key=lambda num : num[1]):
    print('''
    <option value="%s"> %s </option>
    ''' % (key[1], key[0]) )


    but its really frustrating not being able to:

    for key in sorted( months.values() ):
    print('''
    <option value="%s"> %s </option>
    ''' % (months[key], key) )

    Which seemed to be an abivous way to do it.
    names set() was able to order like this why not the dictionary too?
    What is now proved was once only imagined!
  4. #18
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    There is no bug in sorted; it is doing exactly what you ask it to every time. The issue is in selecting the right tool for the job.

    If all your values are consecutive numbers put the items in a list and use the indexes:
    python Code:
    months = ['Ιανουάριος','Φεβρουάριος','Μάρτιος','Απρίλιος','Μάϊος','Ιούνιος',
              'Ιούλιος','Αύγουστος','Σεπτέμβριος','Οκτώβριος','Νοέμβριος','Δεκέμβριος']
     
    for i,month in enumerate(months):
        print('<option value="{}"> {} </option>'.format(i+1,month))

    -Mek
  6. #19
  7. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,281
    Rep Power
    13
    Yes you are doing it with the use of a list though.

    can you present the same thing using a dictionary instead?
    Having sorted() sort out the dictionary by the dict's values?

    for key in sorted( months.values() ):
    print('''
    <option value="%s"> %s </option>
    ''' % (key, how_we_tell_the_cuurent_month_here?) )
    What is now proved was once only imagined!
  8. #20
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    I already showed you how to sort a dict by value even though it is the wrong tool for the job. If you iterate over the values in the dictionary then you won't be able to use the key.

    Use a list. Or do it with an OrderedDict. Or sort the dict the way I previously showed.

    But really, just use a list.
    -Mek
  10. #21
  11. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,281
    Rep Power
    13
    for key in sorted( months.keys() ):
    print('''
    <option value="%s"> %s </option>
    ''' % (months[key], key) )

    this in fact works, it sorts the dict by its keys() was mistaken before
    but the sorting aint correct because its done alphabetically and not by
    integer value.
    We need values.


    So, i want this order to happen by using dic.values(), not list or OrderDict or key lambda

    Code:
    or key in sorted( months.values() ): 
             print(''' 
                     <option value="%s"> %s </option> 
             ''' % (key, what should_we put here_to get the actual months 
    name? )
    since month.keys() work with for why not months.values() too?
    What is now proved was once only imagined!
  12. #22
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    dict.values() returns a list of the values in the dict. Completely unassociated with keys. You cannot through conventional means get the associated key by iterating through the list of values. There is also absolutely no reason to do this.

    You have been shown multiple ways to do what you want and told which is best yet you still want to do it in a way that isn't possible. The key lambda method IS sorting by value. But as I have been trying to point out it is unnecessary here. Your objects belong in a list. For whatever reason you re convinced you need a dict and convinced you need to iterate over dict.values().

    -Mek
  14. #23
  15. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,281
    Rep Power
    13
    Since dict.keys() return a list of the keys in the dict and the keys are associated with the dict's values why doesnt it work the other way around too?

    I'm talking about this:
    Code:
    for key in sorted( months.keys() ): 
             print(''' 
                     <option value="%s"> %s </option> 
             ''' % (months[key], key) )
    I could have uses ordered by kees but that orders key alphabetically, and montha re not displayed in the usual row. ordering by values though it would be a perfect fit.
    Last edited by Nik; June 10th, 2013 at 09:37 AM.
    What is now proved was once only imagined!
  16. #24
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Because if you have a key you can look up the associated value.

    The reverse isn't true though. If you have a value you cannot (directly) look up the associated key.

    -Mek
  18. #25
  19. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,281
    Rep Power
    13
    But 'key<->value' are pairs no? are you usre about this?

    Code:
    for i,month in enumerate(months):
    	print('''
    		<option value="%s"> %s </option>
    	''' % (i + 1, months) )
    can you please explain this? why have 2 iterators i and month and what enumerate does? never used that before.
    What is now proved was once only imagined!
  20. #26
  21. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,281
    Rep Power
    13
    Code:
    for month in enumerate(months):
    	print('''
    		<option value="%s"> %s </option>
    	''' % (month, months[month]) )
    isnt it more easilyt reading it?
    What is now proved was once only imagined!
  22. #27
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Dictionary keys must be unique. But values don't have to be. Multiple keys can store the same value and because of that dictionaries don't work in reverse.

    As for enumerate; enumerate creates a counter you can use as you iterate over a sequence.
    python Code:
    months = ["jan","feb","mar","apr","may","jun",
              "jul","aug","sep","oct","nov","dec"]
     
    for i,month in enumerate(months):
        print("i={} , month={}".format(i,month))
    therefore i+1 is your desired month number.

    -Mek
  24. #28
  25. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,281
    Rep Power
    13
    Code:
    for month in enumerate(months):
    	print('''
    		<option value="%s"> %s </option>
    	''' % (month, months[month]) )
    I prefer this cause i understand it better but it breaks
    Isnt month here iterate over all monts values?
    What is now proved was once only imagined!
  26. #29
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    You can prefer it all you like; it still won't work.

    When you iterate using enumerate you get (counter,object) tuples.
    we assign the counter to i and the object to month when we iterate like this:
    Code:
    for i,month in enumerate(months):
    It is very common to use this technique if it is important to keep track of indexes while iterating.

    -Mek
  28. #30
  29. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,281
    Rep Power
    13
    for i, month in enumerate(months):
    print('''
    <option value="%s"> %s </option>
    ''' % (i, month) )


    this is alos failing. but why? sint i increasing anytine for runs on every element?
    please write it my way without the format...its more easy for me to follow.
    What is now proved was once only imagined!

IMN logo majestic logo threadwatch logo seochat tools logo