Question about dictionaries (Question about is ?) 
Author Message
 Question about dictionaries (Question about is ?)

Hi Everybody-

I've been working with nested dictionaries whose keys are slices of text
from a document, and I've come up with two ways to make them:

Quote:
>>> text = 'spam-a-lam-a-spama'
>>> names = {'spam': text[:4]}
>>> dict1 = {text[:4]: {text[-5:-1]: 'eggs'}}
>>> dict2 = {names[text[:4]]: {names[text[-5:-1]]: 'eggs'}}

with the difference being:

Quote:
>>> dict1.keys()[0] is dict1['spam'].keys()[0]
0
>>> dict2.keys()[0] is dict2['spam'].keys()[0]

1

...but I'm new to programming as well as to Python, so I'm not sure what
this means- does dict2
take up less memory, or something, or is this just a Stupid Programming
Trick? Is this like
intern(..)'ing ?

Thanks in advance,

Felix



Sun, 23 Jun 2002 03:00:00 GMT  
 Question about dictionaries (Question about is ?)

Quote:

> >>> text = 'spam-a-lam-a-spama'
> >>> names = {'spam': text[:4]}
> >>> dict1 = {text[:4]: {text[-5:-1]: 'eggs'}}
> >>> dict2 = {names[text[:4]]: {names[text[-5:-1]]: 'eggs'}}

> with the difference being:

> >>> dict1.keys()[0] is dict1['spam'].keys()[0]
> 0
> >>> dict2.keys()[0] is dict2['spam'].keys()[0]
> 1

> ...but I'm new to programming as well as to Python, so I'm not sure what
> this means-

Holy Dangerous Loops of Logic, Batman! Are you the reincarnation of
Nikola Tesla or what?

I'm not sure what it means either, but what seems to be happening is
that dict1['spam'].keys()[0] returns a reference to text[-5:-1] --
i.e.:

        'spam-a-lam-a-spama'
                    ^^^^
whereas dict2['spam'].keys()[0] returns a reference to text[:4], --
i.e.:

        'spam-a-lam-a-spama'
         ^^^^
... and the two just happen to have the same string value.

Quote:
>>> dict1.keys()[0] == dict1['spam'].keys()[0]

1

Quote:
> does dict2
> take up less memory,

I don't know enough about the internals of python to be sure, but I
can't imagine it would. dict1 references two different objects with
the value 'spam'; dict2 references a single spammy object twice; but
they both contain the same number of object references.

Quote:
> or something, or is this just a Stupid Programming
> Trick?

Hah! Your next challenge is to find a use for this interesting
technique. If you can do that, it won't be just a Stupid Programming
Trick ;-)

--
Matt Gushee
Portland, Maine, USA

http://www.havenrock.com/



Wed, 26 Jun 2002 03:00:00 GMT  
 Question about dictionaries (Question about is ?)

<snip my question>

Quote:
>I'm not sure what it means either, but what seems to be happening is
>that dict1['spam'].keys()[0] returns a reference to text[-5:-1] --
>i.e.:

>        'spam-a-lam-a-spama'
>                    ^^^^
>whereas dict2['spam'].keys()[0] returns a reference to text[:4], --
>i.e.:

>        'spam-a-lam-a-spama'
>         ^^^^
>... and the two just happen to have the same string value.

>>>> dict1.keys()[0] == dict1['spam'].keys()[0]
>1

>> does dict2
>> take up less memory,

>I don't know enough about the internals of Python to be sure, but I
>can't imagine it would. dict1 references two different objects with
>the value 'spam'; dict2 references a single spammy object twice; but
>they both contain the same number of object references.

>> or something, or is this just a Stupid Programming
>> Trick?

>Hah! Your next challenge is to find a use for this interesting
>technique. If you can do that, it won't be just a Stupid Programming
>Trick ;-)

Well the use I have is building a nested dictionary out of
some HTML...near the beginning of the program I make
a dictionary with the tags in the document for keys, so
it's easy to make everything downhill from that point to the
same 20 or so slices of the document instead of pointing
to hundreds ... Which is simpler for me to think about, but
I'm getting the idea that the computer doesn't care.

Quote:

>--
>Matt Gushee
>Portland, Maine, USA

>http://www.havenrock.com/
>--
>http://www.python.org/mailman/listinfo/python-list

Thanks-
        Felix


Wed, 26 Jun 2002 03:00:00 GMT  
 Question about dictionaries (Question about is ?)

Quote:

> >> or something, or is this just a Stupid Programming
> >> Trick?

> >Hah! Your next challenge is to find a use for this interesting
> >technique. If you can do that, it won't be just a Stupid Programming
> >Trick ;-)

> Well the use I have is building a nested dictionary out of
> some HTML...near the beginning of the program I make
> a dictionary with the tags in the document for keys, so
> it's easy to make everything downhill from that point to the
> same 20 or so slices of the document instead of pointing
> to hundreds ... Which is simpler for me to think about, but
> I'm getting the idea that the computer doesn't care.

Well, it's at least a fascinating experiment. And without knowing in
more detail what you're trying to accomplish, it's hard to say for
sure, but my impression is that perhaps you're being a little too
clever.  If you can keep straight which of these indirect object
references is referring to what (as apparently you can) you're smarter
than I am. But when you start trying to process real HTML documents
with all their idiosyncracies, you might have an enormous mess on your
hands.

In summary: if you're just playing around to see how things work,
don't let me or anybody else stop you. But if you are trying to
produce some useful code in a reasonable amount of time, you might
start by looking at how other people have dealt with HTML. If you look
around The Vaults of Parnassus (http://www.vex.net/parnassus/), I'm
sure you'll find something to do with HTML. The XML-SIG might also
give you some ideas.

Best of luck!

--
Matt Gushee
Portland, Maine, USA

http://www.havenrock.com/



Thu, 27 Jun 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Stupid newb question: What am I doing wrong?

2. Question on Clarion 5 and Internet (i am new user)

3. Subject: Re: Newbie question (was Re: I am new)

4. Stupid question I am afraid, but I could use a pointer or two

5. STUPID QUESTION I AM AFRA

6. I am back with a question!

7. Questions... I am new to scheme

8. what am i doing wrong? HLA question

9. Simple Newbie Question: What am I doing wrong?

10. I am back with another PythonCOM Question....!

11. Socket Question : What Am I missing ?

12. Dictionary Question

 

 
Powered by phpBB® Forum Software