473,378 Members | 1,426 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,378 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 1567
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.