Say I have this list - prices = [100.0, 90.0, 90.0, 93.0, 92.0, 80.0, 92.0, 87.0, 83.0, 75.0, 80.0, 70.0, 83.0, 75.0, 80.0, 90.0, 92.0, 85.0]
I created a new list that starts from the low point of the list like so - Right = prices.index(min(prices))
-
midpoint = (min(prices) + max(prices))/2
-
rightprices = prices[right:]
-
print rightprices
[83.0, 75.0, 80.0, 90.0, 92.0, 85.0]
Now I want to cut it up again, but this time taking the numbers the only occur after the midpoint, which should be 85.
How can I get a list that just yields the elements in the list from as soon as a number >= midpoint, thus leaving me with just 90, 82, 85
The way I was doing it before, looks for exactly a certain number (such as the min(price)), if I could only get a greater than clause in there… I tried
Leftover = prices.index(>= midpoint), but that didn't fly.
Any suggestions, ideas, etc?
Thanks
7 1583 bartonc 6,596
Recognized Expert Expert
List comprehensions will be your best friend forever, once you get the hang of them: -
>>> [x for x in prices if x > 83]
-
[100.0, 90.0, 90.0, 93.0, 92.0, 92.0, 87.0, 90.0, 92.0, 85.0]
-
>>>
that's it...kinda.
if i were to add a number to the end of the list, say 20. I'd also want that in the list, because I want all the numbers after a number in the list >= median.
So to do that I was thinking, if I did your method, which would exclude points after the numbers dip below the medium, then I could look for the index of the 1st number in the list. The slice from there...right?
here's what I got and it seems to work. - a = [83.0, 75.0, 80.0, 90.0, 92.0, 85.0, 20.0, 99.0]
-
stuff = [x for x in a if x >= median]
-
newlist = a.index(stuff[0])
-
leftovers = a[newlist:]
leftovers = [90.0, 92.0, 85.0, 20, 99]
BUT
is there a prettier (more pythonic) way to do it?
if you are doing medians, you should sort the data first ,isn't it? -
>>> prices
-
[100.0, 90.0, 90.0, 93.0, 92.0, 80.0, 92.0, 87.0, 83.0, 75.0, 80.0, 70.0, 83.0, 75.0, 80.0, 90.0, 92.0, 85.0]
-
>>> sorted_prices = sorted(prices)
-
>>> sorted_prices
-
[70.0, 75.0, 75.0, 80.0, 80.0, 80.0, 83.0, 83.0, 85.0, 87.0, 90.0, 90.0, 90.0, 92.0, 92.0, 92.0, 93.0, 100.0]
-
>>> median=sorted_prices[len(sorted_prices)/2]
-
>>> print median
-
87.0
-
>>> [i for i in sorted_prices if i > median ]
-
[90.0, 90.0, 90.0, 92.0, 92.0, 92.0, 93.0, 100.0, 90]
-
bvdet 2,851
Recognized Expert Moderator Specialist
if you are doing medians, you should sort the data first ,isn't it? -
>>> prices
-
[100.0, 90.0, 90.0, 93.0, 92.0, 80.0, 92.0, 87.0, 83.0, 75.0, 80.0, 70.0, 83.0, 75.0, 80.0, 90.0, 92.0, 85.0]
-
>>> sorted_prices = sorted(prices)
-
>>> sorted_prices
-
[70.0, 75.0, 75.0, 80.0, 80.0, 80.0, 83.0, 83.0, 85.0, 87.0, 90.0, 90.0, 90.0, 92.0, 92.0, 92.0, 93.0, 100.0]
-
>>> median=sorted_prices[len(sorted_prices)/2]
-
>>> print median
-
87.0
-
>>> [i for i in sorted_prices if i > median ]
-
[90.0, 90.0, 90.0, 92.0, 92.0, 92.0, 93.0, 100.0, 90]
-
You are correct GD. Adding to that, if the length of the sample is an even number, there is no distinct median value. The median value would then be the mean of the two middle values. - def median(s):
-
i = len(s)
-
if i%2:
-
# integer division (e.g. 19/2 = 9)
-
return (s[i/2]+s[(i/2)+1])/2.0
-
return s[i/2]
-
-
prices = [100.0, 90.0, 90.0, 93.0, 92.0, 80.0, 92.0, 87.0, 83.0, 75.0, 80.0, 70.0, 83.0, 75.0, 80.0, 90.0, 92.0, 85.0, 20.0]
-
-
print [i for i in sorted(prices) if i > median(sorted(prices))]
>>> [87.0, 90.0, 90.0, 90.0, 92.0, 92.0, 92.0, 93.0, 100.0]
bartonc 6,596
Recognized Expert Expert
You are correct GD. Adding to that, if the length of the sample is an even number, there is no distinct median value. The median value would then be the mean of the two middle values. - def median(s):
-
i = len(s)
-
if i%2:
-
# integer division (e.g. 19/2 = 9)
-
return (s[i/2]+s[(i/2)+1])/2.0
-
return s[i/2]
-
-
prices = [100.0, 90.0, 90.0, 93.0, 92.0, 80.0, 92.0, 87.0, 83.0, 75.0, 80.0, 70.0, 83.0, 75.0, 80.0, 90.0, 92.0, 85.0, 20.0]
-
-
print [i for i in sorted(prices) if i > median(sorted(prices))]
>>> [87.0, 90.0, 90.0, 90.0, 92.0, 92.0, 92.0, 93.0, 100.0]
Looking at this, I see the list being sorted twice for every element in the list...
I think that patrickc was on the right path in finding the median first: -
>>> prices = [100.0, 90.0, 90.0, 93.0, 92.0, 80.0, 92.0, 87.0, 83.0, 75.0, 80.0, 70.0, 83.0, 75.0, 80.0, 90.0, 92.0, 85.0]
-
>>> def median(floatList):
-
... return sum(floatList)/len(floatList)
-
...
-
>>> median(prices)
-
85.388888888888886
-
>>>
Then apply the list comprehension.
bvdet 2,851
Recognized Expert Moderator Specialist
Looking at this, I see the list being sorted twice for every element in the list...
I think that patrickc was on the right path in finding the median first: -
>>> prices = [100.0, 90.0, 90.0, 93.0, 92.0, 80.0, 92.0, 87.0, 83.0, 75.0, 80.0, 70.0, 83.0, 75.0, 80.0, 90.0, 92.0, 85.0]
-
>>> def median(floatList):
-
... return sum(floatList)/len(floatList)
-
...
-
>>> median(prices)
-
85.388888888888886
-
>>>
Then apply the list comprehension.
Normally you would design your code to sort a list only once, which I did not do. - prices = [100.0, 90.0, 90.0, 93.0, 92.0, 80.0, 92.0, 87.0, 83.0, 75.0, 80.0, 70.0, 83.0, 75.0, 80.0, 90.0, 92.0, 85.0, 20.0]
-
prices.sort()
-
print [i for i in prices if i > median(prices)]
Your function median() calculates the arithmetic mean or average. The median of a list is the middle value of the list after sorting, meaning there are an equal number of values above and below the median.
bartonc 6,596
Recognized Expert Expert
Normally you would design your code to sort a list only once, which I did not do. - prices = [100.0, 90.0, 90.0, 93.0, 92.0, 80.0, 92.0, 87.0, 83.0, 75.0, 80.0, 70.0, 83.0, 75.0, 80.0, 90.0, 92.0, 85.0, 20.0]
-
prices.sort()
-
print [i for i in prices if i > median(prices)]
Your function median() calculates the arithmetic mean or average. The median of a list is the middle value of the list after sorting, meaning there are an equal number of values above and below the median.
Yeah... I didn't pass math... Thanks for straightening me out there.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Hilde Roth |
last post by:
This may have been asked before but I can't find it. If I have
a rectangular list of lists, say, l = ,,], is
there a handy syntax for retrieving the ith item of every sublist?
I know about for i in l] but I was hoping for something more
like l.
Hilde
|
by: Michael Drumheller |
last post by:
(If you're not interested in NumArray, please skip this message.)
I am new to NumArray and I wonder if someone can help me with
array-indexing. Here's the basic situation: Given a rank-2 array
(i.e., a matrix) it seems to be trivial, with array indexing,
to extract a subset of its *columns*. But it does not seem
to be trivial to extract a subset of its *rows*. The code
snippet below describes the problem (if it really is a problem)...
|
by: Steven Bethard |
last post by:
So I need to do something like:
for i in range(len(l)):
for j in range(i+1, len(l)):
# do something with (l, l)
where I get all pairs of items in a list (where I'm thinking of pairs
as sets, not tuples, so order doesn't matter). There isn't really
anything wrong with the solution here, but since Python's for-each
construction is so nice, I usually try to avoid range(len(..)) type
|
by: Ishwor |
last post by:
Hi all. Look at this snippet of code.
>>> l =
>>> l
>>> l
'a'
It prints the value 'a'. Fine so far :-)
l ---> 'a' .
l---> 'a' --> 'a'.
|
by: Bryan Olson |
last post by:
The Python slice type has one method 'indices', and reportedly:
This method takes a single integer argument /length/ and
computes information about the extended slice that the slice
object would describe if applied to a sequence of length
items. It returns a tuple of three integers; respectively
these are the /start/ and /stop/ indices and the /step/ or
stride length of the slice. Missing or out-of-bounds indices
are handled in a manner...
| |
by: Chung Leong |
last post by:
Here's the rest of the tutorial I started earlier:
Aside from text within a document, Indexing Service let you search on
meta information stored in the files. For example, MusicArtist and
MusicAlbum let you find MP3 and other music files based on the singer
and album name; DocAuthor let you find Office documents created by a
certain user; DocAppName let you find files of a particular program,
and so on.
Indexing Service uses plug-ins...
|
by: Derek Peschel |
last post by:
Here are two functions.
def invert_dict_to_lists(dict):
lists = {}
for key in dict:
value = dict
if not value in lists:
lists =
else:
lists.append(key)
|
by: Emin |
last post by:
Dear Experts,
How much slower is dict indexing vs. list indexing (or indexing into a
numpy array)? I realize that looking up a value in a dict should be
constant time, but does anyone have a sense of what the overhead will
be in doing a dict lookup vs. indexing into a list? Some ad hoc tests
I've done indicate that the overhead is less than 15% (i.e., dict
lookups seem to take no more than 15% longer than indexing into a list
and there...
|
by: 78ncp |
last post by:
hi...
how to implementation algorithm latent semantic indexing in python
programming...??
thank's for daniel who answered my question before..
--
View this message in context: http://www.nabble.com/how-to-implementation-latent-semantic-indexing-in-python..-tf4075439.html#a11582773
Sent from the Python - python-list mailing list archive at Nabble.com.
|
by: Rüdiger Werner |
last post by:
Hello!
Out of curiosity and to learn a little bit about the numpy package i've
tryed to implement
a vectorised version of the 'Sieve of Zakiya'.
While the code itself works fine it is astounding for me that the numpy
Version is almost 7 times slower than
the pure python version. I tryed to find out if i am doing something wrong
but wasn't able to find any answer.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |