473,379 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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 1059

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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.