slicing lists

castironpi at gmail.com castironpi at gmail.com
Fri May 9 04:11:02 EDT 2008


On May 9, 1:23 am, "Ian Kelly" <ian.g.ke... at gmail.com> wrote:
> On Wed, May 7, 2008 at 5:29 PM, Yves Dorfsman <y... at zioup.com> wrote:
> > Is there a way to do:
> > x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> > x[0,2:6]
>
> > That would return:
> > [0, 3, 4, 5, 6]
>
> > I am surprised this notation is not supported, it seems intuitive.
> > A concrete example of the sort of thing I want to do:
>
> > p = file('/etc/passwd').readlines()
> > q = [ e.strip().split(':')[0,2:] for e in p ]
>
> > (getting rid of the password / x field)
>
> Have a look at the itemgetter function from the operator module.
>
> g = itemgetter( 0, *range(2, 6) )
> p = file("/etc/passwd").readlines()
> q = [ g( e.strip().split(':') ) for e in p ]

The only thing is, is there is another natural meaning to [a,b:c].

Counting grids on the diagonals, the rational set is well defined:

0: 0, 0
1: 1, 0
2: 0, 1
3: 2, 0
4: 1, 1
5: 0, 2
6: 3, 0
7: 2, 1
...

Thencefore ( 2, 0 ) : ( 3, 0 ) is well defined.  Thencefore,

a,b:c,d

is not; x[a,b:c,d]= x[a]+ x[b:c]+ x[d].



More information about the Python-list mailing list