[melbourne-pug] Lists or Arrays - When do I use what and why - Or : Confused much? I am, Now

Jason j.lee.nielsen at gmail.com
Mon Feb 7 08:23:50 CET 2011

You are correct, you just missed the subtle bit where he made the key of the dictionary out of the whole tuple not just x so the combined (x,y,z) would have to be unique to get a duplicate.

So your lookup would then be something like:

In [1]: points = {}
In [2]: points[1,5,7] = 'data'
In [3]: points
Out[3]: {(1, 5, 7): 'data'}

so the key is the combined data structure or tuple of (1, 5, 7)

This method is nice in that it should be a very fast lookup, Im not sure how much it would limit in your application the ability to lookup a whole dimension at once, ie:


would return the whole Z axis using the array methodology where for this dictionary methodology it would be a bit harder to pull that info out.


On Mon, 07 Feb 2011 18:08:55 +1100, David Crisp <dcrisp at netspace.net.au> wrote:

> Thanks William,
> I read up on the Dictionaries at one of the main points I spotted was a
> reference on one of the sites :
> (a) More than one entry per key not allowed. Which means no duplicate key
> is allowed. When duplicate keys encountered during assignment, the last
> assignment wins.
> By my reading that is saying that there can only be one X value of 10  if
> a new X value of ten comes along then it will supercede the previous.
> Because this is 3D space these points represent,  there can actually be
> more than 1 X of Value 10 and indeed there can be more than 1 Y value of
> 10 as well.
> Or have i misunderstood the restrictions on Dictionaries.
> Regards,
> David
>   On Mon, 7 Feb 2011, William ML Leslie wrote:
>> On 7 February 2011 13:15, David Crisp <dcrisp at netspace.net.au> wrote:
>>> I then wish to iterate through the array and 'bin'  the X values into a
>>> range.  For instance every X value between -54 and -53 goes into bin 1,  -53
>>> to -52 goes into bin 2
>>> Then I wish to iterate through this bin array and sort the Y values into the
>>> same sort of ranges.
>>> Then I wish to Sort the Z values into the same sort of ranges.   This should
>>> give me an array of Array[X][Y][Z] Where Array[0][0][0] should give me the
>>> very first cell  and I can reference any of the values simply by :
>>>>>> print Array[100][100][10]
>>> and get the resulting Z values for that location.
>> A better data structure for an index like this might actually be a
>> dictionary. A dictionary can be keyed on (X, Y, Z) tuples.
>> You could do something just a little neater than than:
>> points = {}
>> for line in file:
>>    points = line.split(None, 6)
>>    if not points[2:]:
>>        # specifically handle blank lines and count lines
>>        continue
>>    x, y, z, i, r, g, b = map(float, points)
>>    region = points.setdefault((int(x), int(y), int(z)), [])
>>    region.append((x, y, z, i, r, g, b))
>> On tuples:
>> Tuples are (generally) for representing heterogeneous data; much like
>> structs or classes in other languages.  Each "column" of your table
> A> represents a different concept; whether an intensity, a location, or
>> whatever, and it looks like they are even supposed to be different
>> types.  Whenever each column has a distinct meaning/type, a
>> heterogeneous collection type like a tuple, namedtuple or class is
>> probably the way to go.
>> On the other hand, you probably want to talk about "the list of
>> points"; dealing with them as a heterogeneous collection.  Each point
>> in the list, or row of your table, represents the same type of data.
>> Someone had suggested doing a talk next month on appropriate use of
>> data structures, so it is probably a good idea to get some questions
>> going on in here.  Using numpy for the detail rows is reasonable too,
>> but that means you either give up unboxing the floats (which unboxing
>> will halve the memory usage of this table on cpython) or give up
>> having different types for different columns; so it might not be
>> worthwhile.
> _______________________________________________
> melbourne-pug mailing list
> melbourne-pug at python.org
> http://mail.python.org/mailman/listinfo/melbourne-pug

More information about the melbourne-pug mailing list