Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #47546 > unrolled thread

Re: Reply to post 'Tryign to add a valkue to a set'

Started byΝικόλαος Κούρας <nikos.gr33k@gmail.com>
First post2013-06-10 13:57 +0300
Last post2013-06-11 00:21 -0700
Articles 9 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Reply to post 'Tryign to add a valkue to a set' Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 13:57 +0300
    Re: Reply to post 'Tryign to add a valkue to a set' Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-06-10 15:14 +0200
      Re: Reply to post 'Tryign to add a valkue to a set' Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 06:37 -0700
        Re: Reply to post 'Tryign to add a valkue to a set' Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-06-10 16:58 +0200
      Re: Reply to post 'Tryign to add a valkue to a set' Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 07:03 -0700
        Re: Reply to post 'Tryign to add a valkue to a set' Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 07:37 -0700
    Re: Reply to post 'Tryign to add a valkue to a set' russ.pobox@gmail.com - 2013-06-10 08:34 -0700
      Re: Reply to post 'Tryign to add a valkue to a set' Νικόλαος Κούρας <nikos.gr33k@gmail.com> - 2013-06-10 08:55 -0700
    Re: Reply to post 'Tryign to add a valkue to a set' russ.pobox@gmail.com - 2013-06-11 00:21 -0700

#47546 — Re: Reply to post 'Tryign to add a valkue to a set'

FromΝικόλαος Κούρας <nikos.gr33k@gmail.com>
Date2013-06-10 13:57 +0300
SubjectRe: Reply to post 'Tryign to add a valkue to a set'
Message-ID<mailman.2963.1370861835.3114.python-list@python.org>
 >Τη Δευτέρα, 10 Ιουνίου 2013 12:40:01 μ.μ. UTC+3, ο χρήστης Ulrich 
Eckhardt έγραψε:

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.


for key in sorted( months.values() ):
         print('''
                 <option value="%s"> %s </option>
         ''' % (key, what should_we put here_to get the actual months 
name? )

[toc] | [next] | [standalone]


#47552

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2013-06-10 15:14 +0200
Message-ID<p20h8a-q0c.ln1@satorlaser.homedns.org>
In reply to#47546
Am 10.06.2013 12:57, schrieb Νικόλαος Κούρας:
>  >Τη Δευτέρα, 10 Ιουνίου 2013 12:40:01 μ.μ. UTC+3, ο χρήστης Ulrich
> Eckhardt έγραψε:
>
> 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()

No, it does not sort the dict. Please slow down, relax and take a look 
at the documentation of sorted(). You are jumping to conclusions based 
on flawed expectations and assumptions, which can only yield garbage in 
the end.

Uli

[toc] | [prev] | [next] | [standalone]


#47556

FromΝικόλαος Κούρας <nikos.gr33k@gmail.com>
Date2013-06-10 06:37 -0700
Message-ID<64451427-9f6f-41fb-a437-e1755c9736f7@googlegroups.com>
In reply to#47552
Τη Δευτέρα, 10 Ιουνίου 2013 4:14:33 μ.μ. UTC+3, ο χρήστης Ulrich Eckhardt έγραψε:
> Am 10.06.2013 12:57, schrieb Νικόλαος Κούρας:
> 
> >  >Τη Δευτέρα, 10 Ιουνίου 2013 12:40:01 μ.μ. UTC+3, ο χρήστης Ulrich
> 
> > Eckhardt έγραψε:
> 
> >
> 
> > 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()
> 
> 
> 
> No, it does not sort the dict. Please slow down, relax and take a look 
> 
> at the documentation of sorted(). You are jumping to conclusions based 
> 
> on flawed expectations and assumptions, which can only yield garbage in 
> 
> the end.

it doe ssort the dict at least for keys() why not for values() too?



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 value

[toc] | [prev] | [next] | [standalone]


#47570

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2013-06-10 16:58 +0200
Message-ID<856h8a-qec.ln1@satorlaser.homedns.org>
In reply to#47556
Am 10.06.2013 15:37, schrieb Νικόλαος Κούρας:
> Τη Δευτέρα, 10 Ιουνίου 2013 4:14:33 μ.μ. UTC+3, ο χρήστης Ulrich Eckhardt έγραψε:
>> Am 10.06.2013 12:57, schrieb Νικόλαος Κούρας:
>>
>>>>Τη Δευτέρα, 10 Ιουνίου 2013 12:40:01 μ.μ. UTC+3, ο χρήστης Ulrich
>>> Eckhardt έγραψε:
>>
>>>
>>
>>> 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()
>>
>>
>>
>> No, it does not sort the dict. Please slow down, relax and take a look
>> at the documentation of sorted(). You are jumping to conclusions based
>> on flawed expectations and assumptions, which can only yield garbage in
>> the end.
>
> it doe ssort the dict at least for keys() why not for values() too?

Well, because it does not sort the dict, it sorts the sequence that you 
pass into sorted(). The dictionary that you retrieved from is not 
modified. Which part of the documentation is unclear to you? Did you 
even bother reading the docs?


> 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.

Listen: Computers will always do what you tell them to. If you tell them 
garbage, they will do garbage. If that is not what you want them to do, 
it's your own fault. That means that you have to precisely(!!!) describe 
what you want when talking to a computer. The computer will not try to 
guess what you might have wanted.

Now, the above claim, that "it sorts the dict by its keys()" is simply 
wrong. Instead, it outputs the dictionary's elements sorted by their 
key. There is a fine distinction between the two. I know what you mean, 
because I'm a human being and I can copy with your vague description, 
but the computer doesn't.

Good luck, I'm outta here....

Uli

[toc] | [prev] | [next] | [standalone]


#47562

FromΝικόλαος Κούρας <nikos.gr33k@gmail.com>
Date2013-06-10 07:03 -0700
Message-ID<318cb046-cba2-41fa-9bd5-934a8ab0afa4@googlegroups.com>
In reply to#47552
Τη Δευτέρα, 10 Ιουνίου 2013 4:14:33 μ.μ. UTC+3, ο χρήστης Ulrich Eckhardt έγραψε:
> Am 10.06.2013 12:57, schrieb Νικόλαος Κούρας:
> 
> >  >Τη Δευτέρα, 10 Ιουνίου 2013 12:40:01 μ.μ. UTC+3, ο χρήστης Ulrich
> 
> > Eckhardt έγραψε:
> 
> >
> 
> > 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()
> 
> 
> 
> No, it does not sort the dict. Please slow down, relax and take a look 
> 
> at the documentation of sorted(). You are jumping to conclusions based 
> 
> on flawed expectations and assumptions, which can only yield garbage in 
> 
> the end.
> 
> 
> 
> Uli

It soerts it just fine by keys() alpabeticall but this isnt what i want, i need sorting by values()

for key in sorted( months.values() ): 
         print(''' 
                 <option value="%s"> %s </option> 
         ''' % (key, what should_we put here_to get the actual months 
name? ) 

the value is a vale form  the values in order, but how do i specify the correspondign key to that value?

[toc] | [prev] | [next] | [standalone]


#47566

FromΝικόλαος Κούρας <nikos.gr33k@gmail.com>
Date2013-06-10 07:37 -0700
Message-ID<5d47d184-618e-4c5c-a7be-0514bd9ca837@googlegroups.com>
In reply to#47562
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) ) 

[/code]

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.

[toc] | [prev] | [next] | [standalone]


#47573

Fromruss.pobox@gmail.com
Date2013-06-10 08:34 -0700
Message-ID<17ba5f17-40d4-4110-832b-8ab021aa57df@googlegroups.com>
In reply to#47546
for key, value in sorted(months.items(), key=lambda x:x[1]):
    print("""'\t<option value"%s">%s</option>'\n""" % (value, key))


Explanation:
- - - - - - 
dict.items is a method associated with dicts just like dict.keys or dict.values, and returns a list of (key, value) pairs.

sorted and some other builtin functions have an optional key argument, which tells the function what exactly to look at when sorting the sequence. (I say sequence because you can sort a string or tuple or dict and get a list back in return). In this case we use a simple lambda function to tell it to look at the value not the key (i.e. (key, value)[1] returns value)

Alternatively you could do this.

def get_value(item):
    return item[1]

for key, value in sorted(months.items(), key=get_value):
    ...etc...

You might also wonder what this would do

for key, value in sorted(months.items()):
    ...etc...

Which becomes a question of what would something like this do

sorted([(1,3), (2,2), (3,1)])

Well sorted is a function that expects to get some iterable (sequence of some kinda items) to sort. If those items happens to be sequences themselves, like (key, value) which is a sequence of two items, then it's only going to care about the first item and ignore the rest (i.e ignore value).

So the above will return
[(1,3), (2,2), (3,1)] which as far as sorted() is concerned, is already sorted.


About the string being printed:
- - - - - - - - - - - - - - - - 
I'm not sure why or if you want a single quotation around the entire element to be printed as well but if so then that's how I'd do it. The \t is for a tab. You also don't have to worry about adding newlines because print will print each statement on a newline anyways by default unless you tell it otherwise by specifying the sep argument.

Sorry my first post, I said allot didn't I.

[toc] | [prev] | [next] | [standalone]


#47575

FromΝικόλαος Κούρας <nikos.gr33k@gmail.com>
Date2013-06-10 08:55 -0700
Message-ID<2853618c-02fc-44eb-842d-a83f047627a8@googlegroups.com>
In reply to#47573
Thanks for answering, i like details os dont worry! :)
Iam about to try your suggestion.

months = { '==========':0, 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 'Μάϊος':5, 'Ιούνιος':6,
           'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 'Νοέμβριος':11, 'Δεκέμβριος':12 }


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

it sorts months alphabeticall but not what i wanted. becuas eosrted months are not the months we know in a row.
we must sort the months valeus isntead.
===================================
and also this (hdaling months as a list instead of a dict)


months = [ '==========', 'Ιανουάριος', 'Φεβρουάριος', 'Μάρτιος', 'Απρίλιος', 'Μάϊος', 'Ιούνιος',
           'Ιούλιος', 'Αύγουστος', 'Σεπτέμβριος', 'Οκτώβριος', 'Νοέμβριος', 'Δεκέμβριος' ]
 

for i, month in enumerate(months):
    print('<option value="%s"> %s </option>' % (i+1, month) ) 

it doesnt produce an error but fails into my script doesnt show results when somthign sleetced.

[toc] | [prev] | [next] | [standalone]


#47646

Fromruss.pobox@gmail.com
Date2013-06-11 00:21 -0700
Message-ID<7749b089-db4c-49ec-bfc5-b9802b3b91eb@googlegroups.com>
In reply to#47546
Just try this in the interpreter and see.

for key, value in sorted(months.items(), key=lambda x:x[1]):
    print "%s %s" % (value, key)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web