accessing elements in multi-dimensional sequences

Duncan Booth me at privacy.net
Fri May 28 08:12:33 EDT 2004


anton at vredegoor.doge.nl (Anton Vredegoor) wrote in 
news:40b72a41$0$128$3a628fcd at reader3.nntp.hccnet.nl:

> d =  ['0891931243\n', '0325443777\n', '0933477028\n',
> '0699624617\n', '0922210996\n']
> 
> #swap rows and columns, with the side effect of turning
> #strings into lists, strings must be of equal length or
> #else some information will be lost:
> 
> d1 = zip(*d)
> 
> #remove a row (corresponds to a *column* in the old view)
> #this is an elementary operation now:
> 
> del d1[-1]
> 
> #swapping again restores the old row and column view:
> 
> d1 = zip(*d1)
> 
> #join the elements of the sublists in order to produce strings:
> 
> d1 = map(''.join,d1)
> 
> print d1
> 
> #output is:
> #['0891931243', '0325443777', '0933477028', '0699624617',
> #'0922210996']

This, of course, only works when the strings are all exactly the same 
length. If they are different lengths it truncates all the strings to the 
length of the shortest.



More information about the Python-list mailing list