Two dimensional lists

Peter Otten __peter__ at web.de
Thu Jan 25 12:18:52 EST 2007


gonzlobo wrote:

> I might get an answer since I didn't call them arrays. :^)
> 
> Ok, I have 2 lists that I need to process individually, then merge
> them into a 2x list and fill with data.
> 
> arinc429 = ['ab', '2b', '0b', '21', 'c1', '61', '11', 'db', '9b', '5b',
> 'eb',
>          '6b', '1b', '6e', '3e']
> iPIDs = [300, 301, 320, 321]
> 
> merged[arinc429, iPIDs]
> 
> # PID & a429 are defined elsewhere
> 
> a_idx = iPIDs.index[PID]
> p_idx = [arinc429.index[a429]
> 
> # shouldn't I be able to fill the lists simply by pointing to a location?
> 
> matrix[a_idx, p_idx] = 0x219 # and so on?

The simplest approach is to go with a dictionary:

>>> matrix = {}
>>> arinc = ['ab', '2b', '0b']
>>> pids = [300, 321]
>>> for x in arinc:
...     for y in pids:
...         matrix[x, y] = y + int(x, 16) # replace with your actual data 
...
>>> matrix
{('ab', 300): 471, ('2b', 300): 343, ('ab', 321): 492, ('0b', 321): 332,
('2b', 321): 364, ('0b', 300): 311}

Get one entry:

>>> matrix["2b", 321]
364

Get all items along an axis:

>>> [matrix[x, 300] for x in arinc]
[471, 343, 311]

Peter



More information about the Python-list mailing list