I wrote:
>orderedListOfTuples = [(k,v) for k,v in sorted(mydict.items())]
>
>(k, mydict[k]) sets off warning bells for me that mydict.items()
>should be in use [ ... ]
But apparently [ x for x in mylist ] doesn't yet trigger such a
warning.
--
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~
Tim Chase wrote:
> > I seem to be unable to find a way to appends more keys/values to the end
> > of a dictionary... how can I do that?
> >
> > E.g:
> >
> > mydict = {'a':'1'}
> >
> > I need to append 'b':'2' to it to have:
> >
> > mydict = {'a':'1','b':'2'}
>
> my understanding is that the order
Magnus Lycka <[EMAIL PROTECTED]> wrote:
>orderedListOfTuples = [(k,mydict[k]) for k in sorted(mydict.keys())]
Or:
orderedListOfTuples = [(k,v) for k,v in sorted(mydict.items())]
(k, mydict[k]) sets off warning bells for me that mydict.items()
should be in use, which are nearly as loud as the on
Magnus Lycka wrote:
> orderedListOfTuples = [(k,mydict[k]) for k in sorted(mydict.keys())]
orderedListOfTuples = sorted(mydict.items())
> It's great that many people try to help out on comp.lang.python,
> the community won't survive otherwise, but I think it's important
> to test answers before
Tim Chase wrote:
> I would lean towards using tuples, as in
>
> ports = [('5631','udp'), ('5632', 'tcp'), ('3389','tcp'), ('5900','tcp')]
>
> which you can then drop into your code:
>
> for (port, protocol) in ports:
> print port, protocol
> #do more stuff
>
> This allows yo
Paul Rubin wrote:
> ports = [('5631', 'udp'),
> ('5632': 'tcp'),
> ('3389': 'tcp'),
> ('5900': 'tcp')]
Change the colons to commas, and this will work...
--
http://mail.python.org/mailman/listinfo/python-list
Tim Chase wrote:
> orderedDict = [(k,mydict[k]) for k in mydict.keys().sorted()]
Wrong. As you stated, sorted() is a function, not a method.
orderedListOfTuples = [(k,mydict[k]) for k in sorted(mydict.keys())]
It's great that many people try to help out on comp.lang.python,
the community won't s
On Tue, 24 Jan 2006 08:45:27 -0600, Tim Chase wrote:
> To get them in a key-order, my understanding is that you have to
> use something like
>
> keys = mydict.keys()
> sort(keys)
> orderedDict = [(k, mydict[k]) for k in keys]
Did you test this before posting?
>>> keys = ["a",
On Tue, 24 Jan 2006 16:01:48 +0100, Yves Glodt wrote:
> that means I can neither have a dictionary with 2 identical keys but
> different values...?
Yes, that's correct.
> I would need e.g. this:
> (a list of ports and protocols, to be treated later in a loop)
>
>
>
> ports = {'5631': 'udp',
>> orderedDict = [(k,mydict[k]) for k in mydict.keys().sort()]
>>
>>Unfortunately, the version I've got here doesn't seem to support
>>a sort() method for the list returned by keys(). :(
>
> I bet it does, but it doesn't do what you think it does. See
> http://docs.python.org/lib/typesseq-mutabl
Tim Chase <[EMAIL PROTECTED]> wrote:
>unless you have a way of doing an in-line sort, in which you
>would be able to do something like
>
> orderedDict = [(k,mydict[k]) for k in mydict.keys().sort()]
>
>Unfortunately, the version I've got here doesn't seem to support
>a sort() method for the li
Yves Glodt:
>I would need e.g. this:
>(a list of ports and protocols, to be treated later in a loop)
[...]
>What would be the appropriate pythonic way of doing this?
In the case of tcp and udp ports, it's the combination of protocol and
port number that's unique, not the port number by itself.
So
Paul Rubin wrote:
> Yves Glodt <[EMAIL PROTECTED]> writes:
>> that means I can neither have a dictionary with 2 identical keys but
>> different values...?
>
> No.
>
>> I would need e.g. this:
>> (a list of ports and protocols, to be treated later in a loop)
>>
>> ports = {'5631': 'udp', '5632': '
Yves Glodt <[EMAIL PROTECTED]> writes:
> that means I can neither have a dictionary with 2 identical keys but
> different values...?
No.
> I would need e.g. this:
> (a list of ports and protocols, to be treated later in a loop)
>
> ports = {'5631': 'udp', '5632': 'tcp', '3389': 'tcp', '5900': 't
> that means I can neither have a dictionary with 2 identical keys but
> different values...?
correct :)
> I would need e.g. this:
> (a list of ports and protocols, to be treated later in a loop)
>
> ports = {'5631': 'udp', '5632': 'tcp', '3389': 'tcp', '5900': 'tcp'}
> #then:
> for port,protoc
Yves Glodt wrote:
> Hi there,
>
> I seem to be unable to find a way to appends more keys/values to the end
> of a dictionary... how can I do that?
>
> E.g:
>
> mydict = {'a':'1'}
>
> I need to append 'b':'2' to it to have:
>
> mydict = {'a':'1','b':'2'}
>
>
>
> How to do?
Sorry for the no
Rene Pijlman wrote:
> Yves Glodt:
>> I seem to be unable to find a way to appends more keys/values to the end
>> of a dictionary
>
> A dictionary has no order, and therefore no end.
that means I can neither have a dictionary with 2 identical keys but
different values...?
I would need e.g. thi
Yves Glodt wrote:
> I seem to be unable to find a way to appends more keys/values to the end
> of a dictionary... how can I do that?
A dictionary doesn't have an 'end' because it is an unordered
collection.
> E.g:
> mydict = {'a':'1'}
> I need to append 'b':'2' to it to have:
mydict['b'] = '2'
p
Yves Glodt wrote:
> I seem to be unable to find a way to appends more keys/values to the end
> of a dictionary... how can I do that?
>
> E.g:
>
> mydict = {'a':'1'}
>
> I need to append 'b':'2' to it to have:
>
> mydict = {'a':'1','b':'2'}
to add stuff to a dictionary, use item assignment:
m
Yves Glodt:
>I seem to be unable to find a way to appends more keys/values to the end
>of a dictionary
A dictionary has no order, and therefore no end.
>mydict = {'a':'1'}
>
>I need to append 'b':'2' to it to have:
>
>mydict = {'a':'1','b':'2'}
>
>How to do?
Like this:
>>> mydict = {'a':'1'}
>
> I seem to be unable to find a way to appends more keys/values to the end
> of a dictionary... how can I do that?
>
> E.g:
>
> mydict = {'a':'1'}
>
> I need to append 'b':'2' to it to have:
>
> mydict = {'a':'1','b':'2'}
my understanding is that the order of a dictionary should never
be rel
Yves Glodt wrote:
> Hi there,
>
> I seem to be unable to find a way to appends more keys/values to the end
> of a dictionary... how can I do that?
>
> E.g:
>
> mydict = {'a':'1'}
>
> I need to append 'b':'2' to it to have:
>
> mydict = {'a':'1','b':'2'}
>
mydict['b'] = '2'
--
http://mail.py
Hi there,
I seem to be unable to find a way to appends more keys/values to the end
of a dictionary... how can I do that?
E.g:
mydict = {'a':'1'}
I need to append 'b':'2' to it to have:
mydict = {'a':'1','b':'2'}
How to do?
Best regards,
Yves
--
http://mail.python.org/mailman/listinfo/pyt
23 matches
Mail list logo