473,656 Members | 2,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is this use of lists normal?

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
Jul 18 '05 #1
1 1210
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
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4500
by: Alan Little | last post by:
I need to merge and de-duplicate some lists, and I have some code which works but doesn't seem particularly elegant. I was wondering if somebody could point me at a cleaner way to do it. Here's my function: +++++++++++++++++++ from sets import Set
10
2171
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted accordingly. Any idea is welcome.
5
7409
by: Clifford W. Racz | last post by:
Has anyone solved the issue of translating lists in Word 2003 (WordML) into xHTML? I have been trying to get the nested table code for my XSLT to work for a while now, with no way to get the collection that I need. To begin, I am using xsltproc that conmes with Cygwin as my processor. I have no particular affinity to this processor except that it is open source and standards compliant. I don't like M$, but if using a M$ processing...
29
3049
by: Joseph Haig | last post by:
I am trying to use descriptive lists, <DL>, as shown in <http://www.maths.man.ac.uk/~jhaig/tmp/test.html> with a style sheet at <http://www.maths.man.ac.uk/~jhaig/tmp/default-2.css>. With Mozilla and Opera they appear correctly, with a border round both the <DT> and <DD> parts but in Internet Explorer the borders are messed up. Can anyone please tell me where the mistake is? As an aside, the <div id="main"> section should have the...
2
8789
by: V_S_H_Satish | last post by:
Dear Friends I am working as oracle and ms sql dba from last 4 years. My company recently migrated to DB2 databases. So i am very much new to db2 database Can any one pls provide script to do daily db2 check lists. The following colums i need in the daily check lists
51
8615
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort for linked lists, I couldn't find one. I read something about Mcilroy's "Optimistic Merge Sort" and studied some implementation, but they were for arrays. Does anybody know if Mcilroys optimization is applicable to truly linked lists at all?
10
1718
by: Richard Maher | last post by:
Hi, Sorry if this is one of those issues that people feel passionate about and I assure you I'm not trolling but rather seeking guidance on which way to go with the web browser presentation of tabular result sets. I'm a newbie and don't know any better, but I immediately opted for Select Lists 'cos A) someone showed me :-) and b) 'cos I didn't think you could select from a Table without a Button-cell on each row (but a recent post of...
0
1076
by: Joeyeti | last post by:
Hi fellow VB knowers (I am but a learner still). I have a question for you which I struggle with. I need to convert nested Lists in MS WORD (whether numbered or bulleted or mixed) from their original format to a tag-formatted text (as used for instance for Wiki articles or phpBB Forums and such). In my particular case I need the text to have basic HTML tags for the basic formatting - e.g. <strong>text</strong> or <em>italics</em> - and this...
19
8601
by: Dr Mephesto | last post by:
Hi! I would like to create a pretty big list of lists; a list 3,000,000 long, each entry containing 5 empty lists. My application will append data each of the 5 sublists, so they will be of varying lengths (so no arrays!). Does anyone know the most efficient way to do this? I have tried: list = ,,,,] for _ in xrange(3000000)]
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8710
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8598
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.