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

martin schweitzer schweitzer.ubiquitous at gmail.com
Mon Feb 7 08:20:45 CET 2011


Hi David

Dictionaries map a key onto a value - and each key must be unique.  However
the keys and values are 'things' and these things are not limited to
integers or strings.  In particular, the value can be a tuple or list... (or
even another dictionary).

So, we can have something like this:

d = {} # d is a dictionary...

d['x'] = (1, 3, 5)  # Tuple
d['y'] = 'abc'       # String
d['z'] = [2, 5, 6]  # list

We can also do something like:

d['x' 'z'] = 42 # degenerates into d['xz'] = 42.

We can also have a tuple as the key... eg.

d[(1,2)] = 42

Hope this helps.

Regards,
Martin




On Mon, Feb 7, 2011 at 6:08 PM, 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
>



-- 
Martin Schweitzer
Mobile: 0412 345 938
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/melbourne-pug/attachments/20110207/b6c4704b/attachment-0001.html>


More information about the melbourne-pug mailing list