Reoedering indexes in list of list

T

Toto

Hello,

I have a list of list
assume myList[x][y] is integer
I would like to create an alias to that list which I could call this
way:
alias[y][x] returns myList[x][y]

how can I do that ? (python 2.6)

(I have a feeling I should use 'property' ;)

Thanks,
--
 
C

Chris Rebert

Hello,

I have a list of list
assume myList[x][y] is integer
I would like to create an alias to that list which I could call this
way:
alias[y][x] returns myList[x][y]

If your "alias" can be read-only:
alias = zip(*myList)

Cheers,
Chris
 
T

Toto

If your "alias" can be read-only:
alias = zip(*myList)



a=[['00','01'],['10','11']]
l=zip(*a)
print(l)

returns... [('00', '10'), ('01', '11')]

IS NOT AT ALL WHAT I WANT ;-)

What I want is

print a[1][0]
'10'
but print l[1][0]
'01'

notice the indexes of the list l are inverted...
 
A

Arnaud Delobelle

Toto said:
If your "alias" can be read-only:
alias = zip(*myList)



a=[['00','01'],['10','11']]
l=zip(*a)
print(l)

returns... [('00', '10'), ('01', '11')]

IS NOT AT ALL WHAT I WANT ;-)

What I want is

print a[1][0]
'10'
but print l[1][0]
'01'

notice the indexes of the list l are inverted...
Ahem...
a = [['00', '01'], ['10', '11']]
l = zip(*a)
a[1][0] '10'
l[1][0]
'01'

Looks *exactly* like what you want to me. It doesn't take that long to
check.
 
A

Andreas Waldenburger

Hello,

I have a list of list
assume myList[x][y] is integer
I would like to create an alias to that list which I could call this
way:
alias[y][x] returns myList[x][y]

how can I do that ? (python 2.6)

(I have a feeling I should use 'property' ;)
The zip thing certainly seems nice. But I can never get my head around
zip quick enough to not be confused by it. So my initial idea would
have been a function:

def reverse_indices(lst, x, y):
return lst[y][x]

(or, possibly, a subclass of list with this as an additional method.)

Granted, the invocation is different from what you want, but I don't
know enough about your problem to judge whether this is an issue.

/W
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top