is this use of lists normal?

Discussion in 'Python' started by Gabriel B., Jan 25, 2005.

  1. Gabriel B.

    Gabriel B. Guest

    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
     
    Gabriel B., Jan 25, 2005
    #1
    1. Advertising

  2. Gabriel B.

    Jeff Shannon Guest

    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
     
    Jeff Shannon, Jan 25, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Durzi
    Replies:
    1
    Views:
    330
    Kevin Spencer
    Dec 17, 2004
  2. JustSomeGuy

    Sorting lists of lists...

    JustSomeGuy, Jun 17, 2004, in forum: C++
    Replies:
    0
    Views:
    326
    JustSomeGuy
    Jun 17, 2004
  3. Sibylle Koczian

    win32com: use not possible as normal user

    Sibylle Koczian, Aug 12, 2005, in forum: Python
    Replies:
    2
    Views:
    493
    Tim Roberts
    Aug 13, 2005
  4. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    410
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  5. Dave
    Replies:
    2
    Views:
    129
Loading...

Share This Page