structure in Python

Bengt Richter bokr at oz.net
Mon Oct 20 21:53:21 EDT 2003


On Mon, 20 Oct 2003 18:13:48 -0500, "Alberto Vera" <avera at coes.org.pe> wrote:

>This is a multi-part message in MIME format.
           ^^^^^^^^--try to send only plain text to newsgroups if you can ;-)
>
>Hello:
>I have the next structure:
>[key1,value1,value2]
>
>['A',1,5]
>['B',6,7]
>
>How Can I make it using Python?
Lots of ways, so how to do it best will depend on how you intend to use it
and e.g., whether you want to make a collection of them be storable and retrievable
after the computer has been off.
>
>How Can I update the value of 6?
Again, how would you like to refer to that 6?

>How Can I insert a key called "C" and its values?
Do you care about the order you are doing it? Do you expect to see what order they
were put in after you have put in many?

>How Can I delete a key called "B"?
What if "B" has many records associated with it? How do you want to specify?

>How Can I search a key called "A"(parameter) and get its values?
Are you saying you want to be able to match more than the "A" here? Will it
be legal to have ['A',1,5] and ['A',1,55] in the same collection? I.e., is any
key1 going to be unique, or not? Will you want to search for e.g, ['A',1, <wild card indicator>]?

As others have shown, a Python dict using key1 as key and the rest as an associated value list
does the basic job for unique keys. But the order is lost, in case you wanted to know what the
first or last item entered was.

All the things can be done. If you want various behavior for some "box" that you
put this info in, I would suggest defining a class and defining the methods to
do the operations on/with the data that you require.

Regards,
Bengt Richter




More information about the Python-list mailing list