Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > is this use of lists normal?

Reply
Thread Tools

is this use of lists normal?

 
 
Gabriel B.
Guest
Posts: n/a
 
      01-25-2005
I just sent an email asking for hints on how to import data into a
python program

As i said earlier i'm really new to python and besides being
confortable with the syntax, i'm not sure if i'm on the right track
with the logic

I'm asking for hints again here at the list because i think i'm
already into premature optimization...

My object model ended up as

DataStorageObj
|-itemsIndex (list, could very well be a set...)
| |-[0] = 0
| |-[1] = 1
| |-[2] = 5
| '-[3] = 6
'-Items (list)
|-[0] = ['cat food', '12,20']
|-[1] = ['dog food', 8,00']
|-[2] = ['dead parrot', '25,00']
'-[3] = ['friendly white bunny', '12,25']

the list itemsindex has the DB index of the data, and the list items
has the data.
So if i want something like "SELECT * FROM items WHERE idx=5" i'd use
in my program
self.items[ self.itemsIndex.index(5) ]
i reccon that's not much nice to use when you're gona do /inserts/ but
my program will just read the entire data and never change it.

Was i better with dictionaries? the tutorial didn't gave me a good
impression on them for custom data...
Tupples? the tutorial mentions one of it's uses 'employee records from
a database' but unfortunatly don't go for it...

i think the 'ideal' data model should be something like
({'id': 0, 'desc': 'dog food', 'price': '12,20'}, ...)
But i have no idea how i'd find some item by the ID within it withouy
using some loops

Thanks!
Gabriel
 
Reply With Quote
 
 
 
 
Jeff Shannon
Guest
Posts: n/a
 
      01-25-2005
Gabriel B. wrote:

> My object model ended up as
>
> DataStorageObj
> |-itemsIndex (list, could very well be a set...)
> | |-[0] = 0
> | |-[1] = 1
> | |-[2] = 5
> | '-[3] = 6
> '-Items (list)
> |-[0] = ['cat food', '12,20']
> |-[1] = ['dog food', 8,00']
> |-[2] = ['dead parrot', '25,00']
> '-[3] = ['friendly white bunny', '12,25']
>
> the list itemsindex has the DB index of the data, and the list items
> has the data.
> So if i want something like "SELECT * FROM items WHERE idx=5" i'd use
> in my program
> self.items[ self.itemsIndex.index(5) ]
> i reccon that's not much nice to use when you're gona do /inserts/ but
> my program will just read the entire data and never change it.
>
> Was i better with dictionaries? the tutorial didn't gave me a good
> impression on them for custom data...
> Tupples? the tutorial mentions one of it's uses 'employee records from
> a database' but unfortunatly don't go for it...


Yes, I think you'd be better off using dictionaries here. You can
spare yourself a level of indirection.

Tuples would be a good way to store the individual items -- instead of
a list containing a name and a price (or so I presume), you'd use a
tuple. Your data storage would then be a dictionary of tuples --

self.items = { 0: ('cat food', '12,20'),
1: ('dog food', '8,00'),
5: ('dead parrot', '25,00'),
6: ('friendly white bunny', '12,25') }

Then your SELECT above would translate to

my_item = self.items[5]

and my_item would then contain the tuple ('dead parrot', '25,00').

Note that the most important difference between tuples and lists, for
this example, is conceptual. Tuples generally express "this is a
collection of different things that are a conceptual group", whereas
lists express "this is a series of similar objects".


> i think the 'ideal' data model should be something like
> ({'id': 0, 'desc': 'dog food', 'price': '12,20'}, ...)
> But i have no idea how i'd find some item by the ID within it withouy
> using some loops


You could use a dictionary for each item, as you show, and then store
all of those in a master dictionary keyed by id -- in other words,
simply replace the tuples in my previous example with a dict like what
you've got here. You could also create a simple class to hold each
item, rather than using small dicts. (You'd probably still want to
store class instances in a master dict keyed by id.)

Generally, any time your problem is to use one piece of information to
retrieve another piece (or set) of information, dictionaries are very
likely to be the best approach.

Jeff Shannon
Technician/Programmer
Credit International


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
lists of lists Jon Slaughter C++ 4 12-13-2004 06:28 PM
Lists of Lists VWWall Computer Information 2 10-21-2004 01:15 AM
Counter for items in lists in lists? Charlotte Henkle Python 8 09-26-2004 04:22 AM
Sorting lists of lists... JustSomeGuy C++ 0 06-17-2004 05:44 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57