473,909 Members | 5,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

indexing in a list

54 New Member
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 1583
bartonc
6,596 Recognized Expert Expert
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
Patrick C
54 New Member
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 Recognized Expert Contributor
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 Recognized Expert Moderator Specialist
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 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.
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 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:
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 Recognized Expert Expert
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
4345
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
6
2754
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)...
12
7034
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
10
2068
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
6521
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...
3
9567
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...
6
1714
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
11618
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...
6
3680
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.
3
6854
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.
0
10037
marktang
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...
0
9879
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,...
0
11348
Oralloy
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...
1
11052
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,...
0
10540
tracyyun
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...
0
6140
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4776
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
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3359
bsmnconsultancy
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...

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.