[Tutor] dictionary datatype
Victor Bouffier
victor at grupocdm.com
Wed Apr 12 07:30:56 CEST 2006
On Tue, 2006-04-11 at 22:37 -0500, Jason Massey wrote:
> Works for me:
>
> >>> dict1 = { 0x2018:u'k', 0x2019:u'd'}
> >>> n = 0x2018
> >>> print dict1[n]
> k
> >>>
>
> On 4/11/06, kakada <hokkakada at khmeros.info> wrote:
> Hello all,
>
> For example, I have a dictionary:
> dict1 = { 0x2018:u'k', 0x2019:u'd'}
>
> I assign:
> n = 0x2018
> print dict1[n]
>
> Then:
> KeyError: '0x2018'
>
> But I can call directly:
> print dict1[0x2018]
>
> So, what is wrong with this? How can I solve it?
>
> Thx
>
> kakada
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
Look into the value of n after assignment:
In [285]: n = 0x2018
In [286]: n
Out[286]: 8216
n is taken as an integer containing the decimal number 8216, which in
hex is represented as 0x2018.
Try again to see if you did not mistakenly typed dict1['0x2018'] or else
defined n as such.
If you try dict1[n], dict1[0x2018], or dict1[8216] you get a correct
result, since the integer variable 'n' contains that value. Trying
dict1['0x2016'] gives you the error because the key does not exist.
define dict1 as:
dict1 = { 0x2018:u'k', 0x2019:u'd'}
and then display it whole:
In [299]: print dict1
{8216: u'k', 8217: u'd'}
Can you see your '0x2018' key anywhere?
HTH
Victor
More information about the Tutor
mailing list