• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 649
  • Last Modified:

Complex data structures in python

I'm looking for an example on how to build and iterate a complex data structure in python.  
The basic structure I need is {x, y, z}   Where x and y are the keys and z is value.   However, z needs to be a data structure in itself and represents 4 separate rows of data.

x and y represent positions on a printed page.  Where x is page # and y is position.   z represents a multi line value that can be printed at X, Y.  

How do build, populate, index and iterate this data structure?
0
tmonteit
Asked:
tmonteit
  • 5
  • 3
  • 2
2 Solutions
 
peprCommented:
The answer depends also on how you get or construct the data.  If the data is from a relational database, it could be better to use directly the database.  If the data is not from a database, it can still be a good idea to populate your own database -- say the sqlite supported via the standard Python module (http://docs.python.org/library/sqlite3.html#module-sqlite3).

If you want to have everything as simple as possible and in your hands, then you should decide what data will be maintained in the built-in types (tendency towards the structured programming approach), and what structures should be captured in the objects of your class (tendency towards object oriented approach).  None of the approaches is better or worse.  It depends on situation.

The built-in data structrures should be taken from bottom up.  Say you have the for rows usually represented as lists (or tuples):

row1 = ['str1', 1, 1.1]
row2 = ['str2', 22, 2.22]
row3 = ['str3', 3, 3.3]
row4 = ['str4', 444, 4.0]

Open in new window


The four rows can be packed again into a list or a tuple (order should be preserved) -- here the tuple:

subtable = (row1, row2, row3, row4)

Open in new window


The x and y are coordinates of a kind -- the tuple is usually used for them:

coord = (5, 23)

Open in new window


It depends whether you want to keep your basic structure accessed associatively.  More likely, you want to build an ordered list of the coord + subtable (or build any list and sort it later).  If the subtable is to be a multiline string, then you can store even the formatted multiline string, or you can create it on the fly.  It depends.  There are too may ways to go.  You should better describe your needs now.


0
 
wmatosCommented:
The simplest way would be to do something like pepr suggested, taking it a bit further:

row1 = ['str1', 1, 1.1]
row2 = ['str2', 22, 2.22]
row3 = ['str3', 3, 3.3]
row4 = ['str4', 444, 4.0]

subtable = (row1, row2, row3, row4) #this is your z

all_data = {}
coords = (5,  23) # this is your x, y

all_data[coords] = z

Open in new window


You can then iterate of this structure like so:
for coords in all_data.keys():
  for row in all_data[coords]:
    #do_something_at(coords) with row.

Open in new window


As pepr stated, there are a million ways to do this.  You'll just have to be specific.

W

0
 
peprCommented:
There is no need to use .keys() when looping through a dictionary.  A dictionary iterator automatically goes through the keys:

for coord in all_data:
    # do something with all_data[coord]

Open in new window


However, I am not sure if you want to use a dictionary here, as it is more difficult to process the items in the order of the coordinates (i.e. page + position on the page).  A list structure may be more suitable here.
0
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
wmatosCommented:
If x is page and y is location, why not iterate over the sorted keys?

for coord in sorted(all_data.keys()):
  #do something

Open in new window


or implicitly (though it can be confusing for newbies... are we sorting the dict or the keys?):
for coord in sorted(all_data):
  #do something

Open in new window


I guess it depends on the level of control required in manipulating/traversing the list of coordinates.  If he simply wants to iterate over it in an ordered form, the listing above works, if he want more granular control then a list structure may be the way to go.
0
 
peprCommented:
One should be careful when sorting anything else than simple-value elements.  But this is what I meant above.  There is probably no good reason to keep the data in a dict.  They can be kept sorted in a list (the coord plus the value as one item).

The all_data.keys() always creates a new list.  Then you are looping through the list and sorting it to another list.  Then you are looping the other list and processing the elements.  Sorting means that there will be at least one more list (but not neccessarily two lists).
0
 
tmonteitAuthor Commented:

I was able to build the data structure no prob.  But I'm having problems indexing a single Entry.   What am I doing wrong here?
all_data[(5,23)]
***KeyError (5,23)

all_data[5,23]
***KeyError (5,23)

Open in new window

0
 
wmatosCommented:
The value must be there to be able to access it in that way.  Otherwise you can try:

z = all_data.get((5,23))

Open in new window


If the key (coordinates (x, y)) is present, it will return the z value.  If not, it will return None.
0
 
tmonteitAuthor Commented:
Awesome!!  Thanks for the help!
0
 
peprCommented:
More precisely, the dict.get() method can take one more argument that is used as the default value.  If you do not use it, the argument, the default value is None.  When using some specific value, you can avoid more complex testing in your code.  (Actually, the testing happens inside the .get() implementation, but your code becomes more readable.)
0
 
peprCommented:
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now