Re: [Dorset] Python - Finding the Minimum Value and its Position in an Array
On Wednesday, 23 January 2019 14:06:29 GMT Ralph Corderoy wrote: > Perhaps that's influencing Google's rankings, although the top hits for > `python find index of minimum item in list' look good to me. Hmmm. That is almost identical to the search that I was performing, but I got nothing useful. I think that Google's rankings are influenced most heavily by previous searches by the person making the query and then by the queries made by the general population. > And then perusing the documentation for the `list' that you had would > give you a grounding in what's built in. > https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-ra > nge I did find that page, but didn't find anything to help me find the index of the min value. Believe it or not :-) I do understand the basics of lists. > (Knowing a language is lots more than its syntax these days. > Familiarity with the standard library is often over half the learning, > especially on smaller, simpler languages, like Go.) Yes. But the real problem for the beginner is knowing that min() can be used on the list object. Once you've got it firmly in your head that *everything* is an object in Python, then you can start to do things like v.index(min(v)) or lowi = l.index(low). I did know that in principle, but I've not got it firmly in my head yet. -- Terry Coles -- Next meeting: BEC, Bournemouth, Tuesday, 2019-02-05 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk/ New thread, don't hijack: mailto:dorset@mailman.lug.org.uk
Re: [Dorset] Python - Finding the Minimum Value and its Position in an Array
Hi Terry, > That works! I'm surprised you're surprised. :-) > Tim also gave me a different technique off list: > > v.index(min(v)) On list, I think; I saw it too after I'd written. It's the same approach as me, just doing the one thing you were having trouble with. > So why were all my searches sending me to pages that used numpy (and > none of them worked) ;-( IMHO Python's on the wane for general-purpose programming. The areas where it's seeing growth is for numerical, statistical, and machine learning, and all of those pull in machine-code libraries to do the heavy work with Python as the `glue' language to orchestrate the labour. Perhaps that's influencing Google's rankings, although the top hits for `python find index of minimum item in list' look good to me. And then perusing the documentation for the `list' that you had would give you a grounding in what's built in. https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range (Knowing a language is lots more than its syntax these days. Familiarity with the standard library is often over half the learning, especially on smaller, simpler languages, like Go.) Cheers, Ralph. -- Next meeting: BEC, Bournemouth, Tuesday, 2019-02-05 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk/ New thread, don't hijack: mailto:dorset@mailman.lug.org.uk
Re: [Dorset] Python - Finding the Minimum Value and its Position in an Array
On Wednesday, 23 January 2019 13:30:39 GMT Ralph Corderoy wrote: > l = [3.125, 3.122, 3.112, 3.126] > for e, i in enumerate(l): > print(e, i) # Just to show you could do it yourself. > > if l: > low = min(l) > lowi = l.index(low) # First if present multiple times. > amean = sum(l) / len(l) # Arithmetic mean. > print(low, lowi, amean) That works! Tim also gave me a different technique off list: v.index(min(v)) So why were all my searches sending me to pages that used numpy (and none of them worked) ;-( -- Terry Coles -- Next meeting: BEC, Bournemouth, Tuesday, 2019-02-05 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk/ New thread, don't hijack: mailto:dorset@mailman.lug.org.uk
Re: [Dorset] Python - Finding the Minimum Value and its Position in an Array
Hi Terry, > As apart of the processing of these numbers, I need to calculate the > average value, the minimum value and the element position of the > minimum value in the List. ... > Vmeas = [3.125,3.122,3.112,3.126] > > I need to use numpy to find which element contains that minimum value. ! l = [3.125, 3.122, 3.112, 3.126] for e, i in enumerate(l): print(e, i) # Just to show you could do it yourself. if l: low = min(l) lowi = l.index(low) # First if present multiple times. amean = sum(l) / len(l) # Arithmetic mean. print(low, lowi, amean) Cheers, Ralph. -- Next meeting: BEC, Bournemouth, Tuesday, 2019-02-05 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk/ New thread, don't hijack: mailto:dorset@mailman.lug.org.uk
Re: [Dorset] Python - Finding the Minimum Value and its Position in an Array
Does v.index(min(v)) do what you want? Tim. */ On Wed, 23 Jan 2019 at 13:21, Terry Coles wrote: > Hi, > > I have a series of measured voltages which are currently being written to > a > Python List. As apart of the processing of these numbers, I need to > calculate > the average value, the minimum value and the element position of the > minimum > value in the List. > > For example, I have got: > > Vmeas = [3.125,3.122,3.112,3.126] > > Using plain old Python, I can calculate the mean and find the minimum > value, > but apparently I need to use numpy to find which element contains that > minimum > value. I found this page: > > > https://www.science-emergence.com/Articles/Find-nearest-value-and-the-index-in-array-with-python-and-numpy/ > > but it doesn't give me the correct element when I run the exact code for a > 1D > array from that site, either in a Python shell as indicated there or as > part > of my program . I just get any old value. > > Can anyone help? > > -- > > > > Terry Coles > > > > -- > Next meeting: BEC, Bournemouth, Tuesday, 2019-02-05 20:00 > Check to whom you are replying > Meetings, mailing list, IRC, ... http://dorset.lug.org.uk/ > New thread, don't hijack: mailto:dorset@mailman.lug.org.uk -- Next meeting: BEC, Bournemouth, Tuesday, 2019-02-05 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk/ New thread, don't hijack: mailto:dorset@mailman.lug.org.uk
[Dorset] Python - Finding the Minimum Value and its Position in an Array
Hi, I have a series of measured voltages which are currently being written to a Python List. As apart of the processing of these numbers, I need to calculate the average value, the minimum value and the element position of the minimum value in the List. For example, I have got: Vmeas = [3.125,3.122,3.112,3.126] Using plain old Python, I can calculate the mean and find the minimum value, but apparently I need to use numpy to find which element contains that minimum value. I found this page: https://www.science-emergence.com/Articles/Find-nearest-value-and-the-index-in-array-with-python-and-numpy/ but it doesn't give me the correct element when I run the exact code for a 1D array from that site, either in a Python shell as indicated there or as part of my program . I just get any old value. Can anyone help? -- Terry Coles -- Next meeting: BEC, Bournemouth, Tuesday, 2019-02-05 20:00 Check to whom you are replying Meetings, mailing list, IRC, ... http://dorset.lug.org.uk/ New thread, don't hijack: mailto:dorset@mailman.lug.org.uk