473,473 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Slicing / subsetting list in arbitrary fashion

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
0 1061

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Dave Benjamin | last post by:
Hey all, I just realized you can very easily implement a sequence grouping function using Python 2.3's fancy slicing support: def group(values, size): return map(None, * for i in...
2
by: Steven Bethard | last post by:
Anyone know why deques don't support slicing? >>> from collections import deque >>> d = deque(i**2 - i + 1 for i in range(10)) >>> d deque() >>> d Traceback (most recent call last): File...
9
by: Jerry Sievers | last post by:
Fellow Pythonists; I am totally puzzled on the use of slicing on mapping types and especially unsure on use of the Ellipsis... and slicing syntax that has two or more groups seperated by comma....
54
by: seberino | last post by:
Many people I know ask why Python does slicing the way it does..... Can anyone /please/ give me a good defense/justification??? I'm referring to why mystring gives me elements 0, 1, 2 and 3...
0
by: Steven Bethard | last post by:
In trying to work out what's different between the start, stop and step of slice.indices() and the start, stop and step of sequence slicing I found that some of the list slicing documentation is...
11
by: John Salerno | last post by:
Given: numbers = can someone explain to me why numbers results in ? I thought the first index, whether going forward or backward, was inclusive. And there is no index of 10 in this...
22
by: bearophileHUGS | last post by:
>From this interesting blog entry by Lawrence Oluyede: http://www.oluyede.org/blog/2006/07/05/europython-day-2/ and the Py3.0 PEPs, I think the people working on Py3.0 are doing a good job, I am...
0
by: Gregg Lind | last post by:
I wish something like this was part of the standard python installation, and didn't require one to use Numpy or Numarray. This sort of list subsetting is useful in many, many contexts.
18
by: John Henry | last post by:
If I have a list of say, 10 elements and I need to slice it into irregular size list, I would have to create a bunch of temporary variables and then regroup them afterwords, like: # Just for...
1
by: Lee Sander | last post by:
hi, i have a list and i can get elements form it via slicing L but sometimes the start is stop i.e. I want to go in the opposite direction,eg L, mattab lets you do L(10:-1:2) to achive this,...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.