Slicing / subsetting list in arbitrary fashion

G

Gregg Lind

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.
 

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,777
Messages
2,569,604
Members
45,226
Latest member
KristanTal

Latest Threads

Top