473,247 Members | 2,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,247 software developers and data experts.

indexing in a list

Say I have this list

Expand|Select|Wrap|Line Numbers
  1. 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

Expand|Select|Wrap|Line Numbers
  1. Right = prices.index(min(prices))
  2. midpoint = (min(prices) + max(prices))/2
  3. rightprices = prices[right:]
  4. 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
Sep 14 '07 #1
7 1563
bartonc
6,596 Expert 4TB
List comprehensions will be your best friend forever, once you get the hang of them:
Expand|Select|Wrap|Line Numbers
  1. >>> [x for x in prices if x > 83]
  2. [100.0, 90.0, 90.0, 93.0, 92.0, 92.0, 87.0, 90.0, 92.0, 85.0]
  3. >>> 
Sep 14 '07 #2
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.

Expand|Select|Wrap|Line Numbers
  1. a = [83.0, 75.0, 80.0, 90.0, 92.0, 85.0, 20.0, 99.0]
  2. stuff = [x for x in a if x >= median]
  3. newlist = a.index(stuff[0])
  4. leftovers = a[newlist:]
leftovers = [90.0, 92.0, 85.0, 20, 99]

BUT
is there a prettier (more pythonic) way to do it?
Sep 14 '07 #3
ghostdog74
511 Expert 256MB
if you are doing medians, you should sort the data first ,isn't it?
Expand|Select|Wrap|Line Numbers
  1. >>> prices
  2. [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]
  3. >>> sorted_prices = sorted(prices)
  4. >>> sorted_prices
  5. [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]
  6. >>> median=sorted_prices[len(sorted_prices)/2]
  7. >>> print median
  8. 87.0
  9. >>> [i for i in sorted_prices if i > median ]
  10. [90.0, 90.0, 90.0, 92.0, 92.0, 92.0, 93.0, 100.0, 90]
  11.  
Sep 15 '07 #4
bvdet
2,851 Expert Mod 2GB
if you are doing medians, you should sort the data first ,isn't it?
Expand|Select|Wrap|Line Numbers
  1. >>> prices
  2. [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]
  3. >>> sorted_prices = sorted(prices)
  4. >>> sorted_prices
  5. [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]
  6. >>> median=sorted_prices[len(sorted_prices)/2]
  7. >>> print median
  8. 87.0
  9. >>> [i for i in sorted_prices if i > median ]
  10. [90.0, 90.0, 90.0, 92.0, 92.0, 92.0, 93.0, 100.0, 90]
  11.  
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.
Expand|Select|Wrap|Line Numbers
  1. def median(s):
  2.     i = len(s)
  3.     if i%2:
  4.         # integer division (e.g. 19/2 = 9)
  5.         return (s[i/2]+s[(i/2)+1])/2.0
  6.     return s[i/2]
  7.  
  8. 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]
  9.  
  10. 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]
Sep 16 '07 #5
bartonc
6,596 Expert 4TB
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.
Expand|Select|Wrap|Line Numbers
  1. def median(s):
  2.     i = len(s)
  3.     if i%2:
  4.         # integer division (e.g. 19/2 = 9)
  5.         return (s[i/2]+s[(i/2)+1])/2.0
  6.     return s[i/2]
  7.  
  8. 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]
  9.  
  10. 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:
Expand|Select|Wrap|Line Numbers
  1. >>> 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]
  2. >>> def median(floatList):
  3. ...     return sum(floatList)/len(floatList)
  4. ... 
  5. >>> median(prices)
  6. 85.388888888888886
  7. >>> 
Then apply the list comprehension.
Sep 16 '07 #6
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:
Expand|Select|Wrap|Line Numbers
  1. >>> 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]
  2. >>> def median(floatList):
  3. ...     return sum(floatList)/len(floatList)
  4. ... 
  5. >>> median(prices)
  6. 85.388888888888886
  7. >>> 
Then apply the list comprehension.
Normally you would design your code to sort a list only once, which I did not do.
Expand|Select|Wrap|Line Numbers
  1. 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]
  2. prices.sort()
  3. 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.
Sep 16 '07 #7
bartonc
6,596 Expert 4TB
Normally you would design your code to sort a list only once, which I did not do.
Expand|Select|Wrap|Line Numbers
  1. 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]
  2. prices.sort()
  3. 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.
Sep 16 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

21
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...
6
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...
12
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,...
10
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'.
108
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...
3
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...
6
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)
4
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...
6
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:...
3
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...
0
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...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
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...
0
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 high-frequency records to 61 million...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
0
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...
0
isladogs
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.