Re: Python Interview Questions
Python Interview Questions and answers... http://net-informations.com/python/iq/default.htm -- https://mail.python.org/mailman/listinfo/python-list
Re: The following modules appear to be missing ['_sysconfigdata']
Well that escalated quickly :). So, no-one can tell me which component to pip to get rid of "missing ['_sysconfigdata']" error? -- https://mail.python.org/mailman/listinfo/python-list
How can I find the indices of an array with float values in python?
I have an array (numpy.ndarray) with shape (1500L,) as below: x = array([ 3.e+01, 6.e+01, 9.e+01, ..., 4.4940e+04, 4.4970e+04, 4.5000e+04]) Now, I wanted to determine the indices of the x values between 0.0 and 15.0. While this is simple in MATLAB or IDL by using find or where functions, I was unable to find a best way to find the indices of all elements between 0.0 and 15.0 in the x array. Can you please suggest me a solution for the same? -- https://mail.python.org/mailman/listinfo/python-list
Re: How can I find the indices of an array with float values in python?
Sorry for re-posting with a correction. I have an array (numpy.ndarray) with shape (1500L,) as below: x = array([ 3.e+01, 6.e+01, 9.e+01, ..., 4.4940e+04, 4.4970e+04, 4.5000e+04]) Now, I wanted to determine the indices of the x values between 0.0 and 15000.0. While this is simple in MATLAB or IDL by using find or where functions, I was unable to find a best way to find the indices of all elements between 0.0 and 15000.0 in the x array. Can you please suggest me a solution for the same? -- https://mail.python.org/mailman/listinfo/python-list
RE: How can I find the indices of an array with float values in python?
Maybe something along the lines of... indices = [i for i, val in enumerate(x) where 0 <= val <= 15000] -Original Message- From: Python-list [mailto:[email protected]] On Behalf Of Madhavan Bomidi Sent: Thursday, January 10, 2019 12:05 PM To: [email protected] Subject: Re: How can I find the indices of an array with float values in python? Sorry for re-posting with a correction. I have an array (numpy.ndarray) with shape (1500L,) as below: x = array([ 3.e+01, 6.e+01, 9.e+01, ..., 4.4940e+04, 4.4970e+04, 4.5000e+04]) Now, I wanted to determine the indices of the x values between 0.0 and 15000.0. While this is simple in MATLAB or IDL by using find or where functions, I was unable to find a best way to find the indices of all elements between 0.0 and 15000.0 in the x array. Can you please suggest me a solution for the same? -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: How can I find the indices of an array with float values in python?
Madhavan Bomidi wrote: > I have an array (numpy.ndarray) with shape (1500L,) as below: > > x = array([ 3.e+01, 6.e+01, 9.e+01, ..., > 4.4940e+04, 4.4970e+04, 4.5000e+04]) > > Now, I wanted to determine the indices of the x values between 0.0 and > 15.0. While this is simple in MATLAB or IDL by using find or where > functions, I was unable to find a best way to find the indices of all > elements between 0.0 and 15.0 in the x array. Can you please suggest me a > solution for the same? Like this? >>> a = numpy.array([-1, 10, 100, 5, 1000.]) >>> numpy.where((a < 15) & (a > 0))[0] array([1, 3]) -- https://mail.python.org/mailman/listinfo/python-list
Re: How can I find the indices of an array with float values in python?
On 1/10/19 9:25 AM, Peter Otten wrote: Madhavan Bomidi wrote: I have an array (numpy.ndarray) with shape (1500L,) as below: x = array([ 3.e+01, 6.e+01, 9.e+01, ..., 4.4940e+04, 4.4970e+04, 4.5000e+04]) Now, I wanted to determine the indices of the x values between 0.0 and 15.0. While this is simple in MATLAB or IDL by using find or where functions, I was unable to find a best way to find the indices of all elements between 0.0 and 15.0 in the x array. Can you please suggest me a solution for the same? Like this? a = numpy.array([-1, 10, 100, 5, 1000.]) numpy.where((a < 15) & (a > 0))[0] array([1, 3]) I was going to suggest numpy.nonzero, which gives the same result as the simple case of numpy.where as above. In either case, the result is a tuple of indices per axis. >>> a = [[2, 7, 6], [9, 5, 1], [4, 3, 8]] >>> a = np.array([[2, 7, 6], [9, 5, 1], [4, 3, 8]]) >>> b = np.array([1, 5, 2, 6, 3, 7, 4, 8, 5, 9]) >>> (a > 5).nonzero() (array([0, 0, 1, 2]), array([1, 2, 0, 2])) >>> np.nonzero(b % 2) (array([0, 1, 4, 5, 8, 9]),) -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix. -- https://mail.python.org/mailman/listinfo/python-list
Re: How can I find the indices of an array with float values in python?
On 2019-01-10 17:05, Madhavan Bomidi wrote: Sorry for re-posting with a correction. I have an array (numpy.ndarray) with shape (1500L,) as below: x = array([ 3.e+01, 6.e+01, 9.e+01, ..., 4.4940e+04, 4.4970e+04, 4.5000e+04]) Now, I wanted to determine the indices of the x values between 0.0 and 15000.0. While this is simple in MATLAB or IDL by using find or where functions, I was unable to find a best way to find the indices of all elements between 0.0 and 15000.0 in the x array. Can you please suggest me a solution for the same? I don't know if there's a better way, but: >>> import numpy as np >>> x = np.array([ 3.e+01, 6.e+01, 9.e+01, 4.4940e+04, 4.4970e+04, 4.5000e+04]) Unfortunately, chained comparisons aren't supported by numpy, so: >>> np.logical_and(0.0 <= x, x <= 15000.0) array([ True, True, True, False, False, False]) Now for the indices: >>> np.arange(x.shape[0]) array([0, 1, 2, 3, 4, 5]) Extract the indices where the Boolean value is True: >>> np.extract(np.logical_and(0.0 <= x, x <= 15000.0), np.arange(x.shape[0])) array([0, 1, 2]) -- https://mail.python.org/mailman/listinfo/python-list
Re: the python name
On 8/01/19 4:59 PM, rbowman wrote:> On 01/07/2019 02:10 PM, DL Neil wrote: >> Why is that obscure? It makes perfect sense - to those of us who have >> used tape/serial storage! Perhaps less-so to [bobble-heads], sorry I >> mean people who grew-up with 'bubble memory' (Memory sticks, 'flash >> drives', SSDs). In point-of-fact, Python Context Managers > > Bubble memory, now there is a blast from the past... More like a whimper... Working with a bunch of younger folk (who may technically be of the age of 'grand-children' - pardon me, I almost fell over my (long, grey) beard), I am frequently the butt of their gentle, if ageist, jokes. However, my sardonic amusement is to watch them wrestling with concepts 'we' readily recognise, perhaps from as long ago as mainframe-days. A similar observation relates to technologies which have come-and-gone, eg CCDs, and the bleeding-edge/crest-of-the-wave philosophies that it may not be worth rushing into every new technology - because it may not actually last the distance... (Santayana's "Those who cannot remember [learn from] the past are condemned to repeat it." - my addition in list-brackets*) * which was added only to be able to make tenuous claim of relevance to a Python discussion list! -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: the python name
On Fri, Jan 11, 2019 at 6:48 AM DL Neil wrote: > Working with a bunch of younger folk (who may technically be of the age > of 'grand-children' - pardon me, I almost fell over my (long, grey) > beard), I am frequently the butt of their gentle, if ageist, jokes. > However, my sardonic amusement is to watch them wrestling with concepts > 'we' readily recognise, perhaps from as long ago as mainframe-days. My Dad was in computing back in the mainframe days, and used to regale me with stories of holding a kilobyte of memory. But now I've started doing the same sorts of things - I can expound upon the wonders of engineering floppy disk boot sectors and finding that one spare byte that I could use for something unrelated, or writing MS-DOS TSRs, or *avoiding* writing a TSR by having it invoke another program and then clean up after itself when that program finished... I distinctly remember hooking the Print Screen interrupt to switch from one screen to another so that I could play Colossal Caves (aka "ADVENTUR") and keep track of which treasures I'd collected. Young folks these days would say "why couldn't you just alt-tab"... ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: the python name
On 8/01/19 12:04 PM, Dennis Lee Bieber wrote:
On Tue, 8 Jan 2019 10:10:13 +1300, DL Neil
declaimed the following:
Why is that obscure? It makes perfect sense - to those of us who have
used tape/serial storage! Perhaps less-so to [bobble-heads], sorry I
mean people who grew-up with 'bubble memory' (Memory sticks, 'flash
drives', SSDs). In point-of-fact, Python Context Managers
Apologies, but "bubble memory" is something completely different --
using movable magnetic domains rather than capacitive charged bits.
You are correct.
Although, back in the days of such excitement: that we had a
rapidly-approaching possibility of a cost-effective alternative to other
forms of memory - particularly 'spinning rust'; only the electrical
engineers worried too much about the underlying technologies. In fact,
many of the early devices were too optimistic and didn't enjoy
longevity. However, with persistence (hah!) here we are today, taking
SSDs for granted...
As mentioned 'elsewhere', that particular team I joined had their
average-age considerably elevated when I arrived. As such an object of
curiosity, one of the things noticed was that I ("pomodoro") take
regular seat-breaks to stretch and move. Apparently, the arm and
shoulder-loosening movements gained fans, so one tea-break I arrived to
find them all lined-up around the room, with joined hands, doing a
night-club dance version of the Brazilian Wave and giggling insanely.
However, as some (already) suffer neck and head-aches, I was(seriously)
asked for ideas to combat that common oppression. I taught them to
rotate the head (slowly, but fully) sideways (in the fashion of a
westerner saying "no") + three repeats, then to nod (westerner saying
"yes") etc, and finally to 'waggle' the top of the head side-to-side
(Indian "agreement").
Perhaps you can imagine the next tea-time, dance-floor routine! In
between chuckles, I referred to them as a bunch of "bubble-heads". (with
some dispute as to whether I actually said "bobble-heads", but the term
took their fancy and entered into common parlance)...
At about the same time, someone turned-up with a bust of Mozart,
Beethoven, or some-such; making a point about having stiff necks that
felt solid... The next thing I knew, someone else has added to the
collection, a particularly ugly bobble-head doll. A day or so later,
someone else 'improved' on it with a bobble-headed dog...
Finally, during a meeting last week (just before this conversation
on-list), someone made the mistake of describing an incomplete idea as
"bubbling-up in my mind", which utterly brought the house down!
With lightning wit (moving at a speed which belies my years) I took full
advantage by claiming that not just he, but they all have having
bobble/bubble memories!
(you don't have to be mad to work here - but it helps!)
OK, so that's more than you ever wanted to know about why I (erroneously
or otherwise) used such a term...
Refs:
https://www.youtube.com/watch?v=v2E8f-_ERl0
http://www.bobbledad.com/man-in-mankini-bobblehead
https://www.ebay.com/sch/i.html?LH_CAds=&_ex_kw=&_fpos=&_fspt=1&_mPrRngCbx=1&_nkw=bobble+head+dog&_sacat=&_sadis=&_sop=12&_udhi=&_udlo=&_fosrp=1
(here do we stumble upon the reason eBay's server is called "rover"?)
Why "print"? I thought I was displaying something on the screen (indeed
STDIO might go to a file, eg log-like). Whither print?
Using the wrong language then... COBOL has DISPLAY
Indeed, and it survived into the CICS/terminal-based era!
(although these days I recommend handing-off to HTML as a front-end - I
guess then I should observe that HTML has no command to display/print!)
Why "print" -- least surprise! Languages back to FORTRAN (and predating
terminals) had "print" as the quick&dirty output statement (vs the more
complex formatted output). FORTRAN, BASIC, Pascal, probably Modula-2.
"Print" only has "least-surprise" to those of us who can claim a history
like yours/ours. When we used Teletype terminals and all user
(human-readable) output went to a line printer, then the word was
appropriate. To programming neophytes it makes no sense whatsoever, any
more than would "type".
From your own description, what about "output"?
DISPLAY works for me. I've heard folk talk about "emit", there's
"write"*, and "type", "present", "exhibit" - from photography
"expose"... Going to an ink-jet printer could we say "spit" or to a
laser, "burn"?
Referring back to an earlier point, I don't "print" but have developed
the habit of using another, specific function/method to "log"!
* with the recent move to f-strings (Py3.3 was it? - haven't gone back
to look it up) and the articles discussing their advantages over
what-went-before, reminded me very much of the essential logic of
FORTRAN's READ/WRITE and FORMAT statements!
Reminds me of the person who upon being told to move his mouse to the
top of the screen, picking-up the device
Re: the python name
On 11/01/19 8:57 AM, Chris Angelico wrote: On Fri, Jan 11, 2019 at 6:48 AM DL Neil wrote: Working with a bunch of younger folk (who may technically be of the age of 'grand-children' - pardon me, I almost fell over my (long, grey) beard), I am frequently the butt of their gentle, if ageist, jokes. However, my sardonic amusement is to watch them wrestling with concepts 'we' readily recognise, perhaps from as long ago as mainframe-days. My Dad was in computing back in the mainframe days, and used to regale me with stories of holding a kilobyte of memory. But now I've started doing the same sorts of things - I can expound upon the wonders of engineering floppy disk boot sectors and finding that one spare byte that I could use for something unrelated, or writing MS-DOS TSRs, or *avoiding* writing a TSR by having it invoke another program and then clean up after itself when that program finished... I distinctly remember hooking the Print Screen interrupt to switch from one screen to another so that I could play Colossal Caves (aka "ADVENTUR") and keep track of which treasures I'd collected. Young folks these days would say "why couldn't you just alt-tab"... Ah yes, the ?good, old, days! Things were so much better then, before 'young experts' came along and complicated 'everything'... However, as I rock my porch-chair and reminisce, I find that even nostalgia ain't as good as it once used to be... PS the smart reply: who do you think coded the Alt-Tab window-switching mechanism? or, whose shoulders' do you young, whipper-snappers think you're standing on? (and, "please get down, and go and wash your feet") ... -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: the python name
On Fri, Jan 11, 2019 at 8:01 AM DL Neil wrote: > PS the smart reply: who do you think coded the Alt-Tab window-switching > mechanism? > or, whose shoulders' do you young, whipper-snappers think you're > standing on? (and, "please get down, and go and wash your feet") Hold on hold on. "Window-switching mechanism"? That implies that you have windows to switch between. First, we have to have the concept of multiple programs running at once. Then the concept of each of those programs having its own isolated display, which you can then switch between... But yeah, "whose shoulders are you standing on" is the right way to look at it. Also: "are you offering YOUR shoulders to the next generation?" ChrisA -- https://mail.python.org/mailman/listinfo/python-list
RE: How can I find the indices of an array with float values in python?
Madhavan,
Others have given you reasonable answers out of the ever so many many many
ways you can do what you asked. I offer a question to consider in case your
needs are different or you have not considered other more pythonic ways.
What do you want to do with your data and is this the best way to do it?
The pythonic way often is to minimize the use of features common in
languages like C and use new paradigms like iteration where no index is
needed. As others show, you can do many things like a list comprehension
with an if, perhaps alongside an enumerate to catch the index too.
So, you have an array of numbers. (numpy style) and you want a way to
designate a subset of those numbers that meet your criterion. Your criterion
is a compound criterion that needs refining. You want numbers between 0 and
15. Are you including one or both endpoints? Any answers you choose to use
will need to be adjusted depending on your answer.
And, you seem to want not the values, but instead the indices of the array
where those values are stored. Are you sure that is what you need to go to
the next step? Maybe you do under your currently conceived algorithm and the
solutions provided will give you a second array of indices you can use in an
unspecified way later. But if all you want to do is get a potentially
smaller array containing just the values you want, and perhaps iterate on
them or apply a function such as getting the standard deviation, there is
another set of answers that just makes copies of the array in any way you
want, sometimes in multiple steps.
I choose to illustrate below with what some may consider a tutorial and
ignore. If your needs are met, feel free.
Is a numpy array the best data type for your needs. If you were using a
pandas DataFrame where one column was your numpy array and you had other
columns, then one could be an index of N numbers corresponding to your
requested index. If you used commands to create a second DataFrame with only
those rows that matched your criteria, the resulting DataFrame would
automatically have the index numbers, and perhaps much more.
I am NOT saying the added complications are needed, just that without
knowing your goals, it may be other ways are a better fit.
And note that in numpy (and pandas) there are other ways to index. Yes, you
can provide an array of integers to index with like this:
import numpy as np
# Create an array to play with odd numbers.
odds = np.array(range(1,20,2))
odds
==> array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19])
# Show indexing of the array using indices.
three_in = [2, 4, 6]
odds[three_in]
==> array([ 5, 9, 13])
But you can also index by a Boolean (True/False) list of the same size like:
# Show Boolean indices
three_bool = [False, False, True, False, True, False, True, False, False,
False]
odds[three_bool]
==> array([ 5, 9, 13])
Same result.
Now you seem to want numbers in a range. I show a way in this example to get
everything greater than one number but less than or equal to another. Since
my numbers happen to be in order, ...
# Show how to get numbers in a range.
odds[(odds > 3) & (odds <= 15)]
==> array([ 5, 7, 9, 11, 13, 15])
So if you don't need indices, something like the above gives you the data
itself to use. If your data must keep track of multiple sets of data, and
you need an index to deal with them, I would suggest another data structure.
Here for example, I combine the same vector as above along with a list of my
favorite elements
# Make a DataFrame with an additional field and index.
import pandas as pd
ind = np.array(range(len(odds)))
elementals = np.array(["Hydrogen", "Lithium", "Boron",
"Nitrogen", "Fluorine", "Sodium",
"Aluminum", "Phosphorus", "Chlorine",
"Potassium"])
dictate = { "indexed" : ind, "protons" : odds, "moniker": elementals }
df = pd.DataFrame(dictate)
df
==>
indexed protons moniker
0 0 1 Hydrogen
1 1 3 Lithium
2 2 5 Boron
3 3 7 Nitrogen
4 4 9 Fluorine
5 5 11 Sodium
6 6 13 Aluminum
7 7 15 Phosphorus
8 8 17 Chlorine
9 9 19 Potassium
In the above, note there is a default index that happens to match what I
chose. The point is that if you wanted a subset including everything in a
row, you do not really need the index:
df[(df.protons > 5) & (df.protons <= 15)]
==>
indexed protons moniker
3 3 7 Nitrogen
4 4 9 Fluorine
5 5 11 Sodium
6 6 13 Aluminum
7 7 15 Phosphorus
But if you did not ever need the index, you can not include it or even
suppress the default one.
My point, as I stop here from giving the entire tutorial, is for you to
think about what you want to do and consider which way to do it and then use
reasonable tools. If you really
Re: How can I find the indices of an array with float values in python?
Thank you all for various ways of finding the indices. -- https://mail.python.org/mailman/listinfo/python-list
Re: the python name
Chris, On 11/01/19 10:06 AM, Chris Angelico wrote: On Fri, Jan 11, 2019 at 8:01 AM DL Neil wrote: PS the smart reply: who do you think coded the Alt-Tab window-switching mechanism? or, whose shoulders' do you young, whipper-snappers think you're standing on? (and, "please get down, and go and wash your feet") Hold on hold on. "Window-switching mechanism"? That implies that you have windows to switch between. First, we have to have the concept of multiple programs running at once. Then the concept of each of those programs having its own isolated display, which you can then switch between... Before Windows or windows per-se, we had switching between Foreground and Background - but using those terms today encourages people to start raving about Themes and color choices... I had people 'on the hook' for quite a while talking about WIMPs. They thought I was into some long-winded, shaggy-dog style of joke. They had no concept that what they think of as MS-Windows, or indeed Linux windows managers; actually consists of a number of inter-related concepts... But yeah, "whose shoulders are you standing on" is the right way to look at it. Also: "are you offering YOUR shoulders to the next generation?" +1 Excellent! (and no, in our 'get it done yesterday' environment I see plenty of collaboration but little meaningful 'training' or investment beyond 'the job') -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list
