Jumping around when assigning elements
Andrew Bennetts
andrew-pythonlist at puzzling.org
Mon Dec 15 22:04:10 EST 2003
On Mon, Dec 15, 2003 at 06:57:19PM -0800, Matthew Sims wrote:
[...]
>
> So far the only way I could get this to work in Python was to insert
> "" to represent an empty slot. But Python counts "" when I want to see
> how many tapes are currently taking up slots "len(SLOTS)". It would
> always show 40. So I now know that I need to write code to tell Python
> not to count "". Not too difficult.
>
> I guess I'm realizing how I need to approach Python as my first OO.
> I'm re-writing the script as sort of a training. The program works
> flawlessly in Perl, I'm just doing it to learn. So far it has taken
> less lines of code in Python to perform the same tasks in Perl...up
> til this point. ;)
It sounds like your problem isn't OO, but the wrong choice of datastructure.
As someone else in this thread has already said, you want a dictionary
(known as a 'hash' in perl), not a list. Among other things, len(some_dict)
will give you the result you expect.
-Andrew.
More information about the Python-list
mailing list