Slicing / subsetting list in arbitrary fashion

Gregg Lind gregg at lind-beil.net
Fri Nov 17 17:58:12 CET 2006


One difficulty I am having with using Python for scientific computing is 
that I cannot figure out good ways to get arbitrary (unpatterned?) slices.

As an example, in R or Matlab / Octave, syntax exists such that:

vals = range(6)
wanted = [1,2,3,1,1,1]
vals[wanted] = [1,2,3,1,1,1]

Both of those languages also allow for using filter-like functionality:

Truths = [True,False,False,False,True,True]
valse[Truths] = [0,4,5]

In Python, solutions I have found for these tasks are:

[vals[ii]  for ii in wanted]    # task 1

[a[1]  for  in zip(Truths,vals) if  a[0] ]      # task 2


I find neither of these to be very satisfying.  Is there some other 
method for doing this?  I was unable to find a PEP relating to this, and 
would appreciate any help the combined brains of the Python world can give.

Gregg L.






More information about the Python-list mailing list