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 1563
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
Expert Mod 2GB
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]
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
Expert Mod 2GB
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.
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...

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
arrayindexing. Here's the basic situation: Given a rank2 array...

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,...

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...

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...

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...

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:...

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...

by: abbasky 
last post by:
### Vandf component communication method one: data sharing
Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...

by: stefan129 
last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multidomain SSL certificates? Any recommendations on reliable providers or specific...

by: davi5007 
last post by:
Hi,
Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

by: DolphinDB 
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion highfrequency records to 61 million...

by: Aftab Ahmad 
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

by: ryjfgjl 
last post by:
ExcelToDatabase: batch import excel into database automatically...

by: isladogs 
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...

by: marcoviolo 
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

by: isladogs 
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
 