By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,404 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,404 IT Pros & Developers. It's quick & easy.

Slicing / subsetting list in arbitrary fashion

P: n/a
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.

Nov 17 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.