help regarding extracting a smaller list from larger one
gherron at digipen.edu
Fri Sep 16 00:25:18 CEST 2011
On 09/15/2011 11:55 AM, neeru K wrote:
> Dear Gary,
> thank you for the reply.
> I will be more specific and take the same example that you have given
> and tell you what is my problem -
> array([0.0, 1.3, 2.45, 3.87, 4.54, 5.11, 6.90, 7.78, 8.23, 9.01])
> from this array I want to a sub-list with lower and upper indexes that
> are not present in the above array for example - sub-list[3, 8]
> Is there a function for this?
> thank you
You say "index", but I think that is not what you mean. An index is in
integer referring to an element's position in the array. The "value"
at an index is what I believe you are referring to.
Nevertheless, I think list comprehension is what you want:
[ x for x in A if 3.0 <= x <= 8.0 ]
will extract all the values between 3 and 8 from the array A and create
a new list containing them.
If you want the new list to be a numpy array then
numpy.array([ x for x in A if 3.0 <= x <= 8.0 ])
> On Thu, Sep 15, 2011 at 10:54 PM, Gary Herron <gherron at digipen.edu
> <mailto:gherron at digipen.edu>> wrote:
> On 09/15/2011 09:40 AM, neeru K wrote:
> Dear Python Users,
> I am trying to write a code for visualization (raster plots
> and peri-event time histogram) of time series
> electrophysiological data using numpy, scipy and matlplotlib
> in python. I am importing the data into list using loadtext
> I was curious if anyone is aware of a function in numpy that
> can *extract a smaller list containing numbers from a larger
> list* given the upper and lower limit of the values between
> which the smaller list lies.
> Thank you in advance.
> Niranjan Kambi
> Senior Research Fellow,
> Neeraj Lab,
> National Brain Research Centre,
> Manesar, Gurgaon-122050
> Haryana, INDIA
> Ph:+919818654846 <tel:%2B919818654846>
> email:- neuro.niru at nbrc.res.in <mailto:neuro.niru at nbrc.res.in>
> <mailto:neuro.niru at nbrc.res.in
> <mailto:neuro.niru at nbrc.res.in>>, neuroniru at gmail.com
> <mailto:neuroniru at gmail.com> <mailto:neuroniru at gmail.com
> <mailto:neuroniru at gmail.com>>
> Do mean to extract a sub-list from a list based on lower and upper
> indexes into the list? Python has a standard notation for
> indicating sub-lists, and numpy implements them:
> >>> a = numpy.array(range(10))
> >>> a
> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
> >>> a[3:6]
> array([3, 4, 5])
> If you mean something else, please be more specific, and we'll try
> Gary Herron
> Gary Herron, PhD.
> Department of Computer Science
> DigiPen Institute of Technology
> (425) 895-4418 <tel:%28425%29%20895-4418>
Gary Herron, PhD.
Department of Computer Science
DigiPen Institute of Technology
More information about the Python-list