Results 1 to 6 of 6

Thread: How to find first index in list i.e. greater than x in python program?

  1. #1
    Join Date
    Nov 2009
    Posts
    50

    How to find first index in list i.e. greater than x in python program?

    Hello friends,
    I am recently started learning python language. Can any one tell me is there any way in python to find the first index in a list that is greater than x?

    For example, with
    list = [5, 3, 9, 8]
    the function
    f(list, 7)
    would give output= 2. It means third one is greater in list. Please help me.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to find first index in list i.e. greater than x in python program?

    You have to use following code in your program to find first index in list i.e. greater than x . It is very simple code.

    Code:
    next(x[0] for x in enumerate(Ls) if x[1] > 7)
    In the above code I have avoid the magic numbers like next(idx for idx, value in enumerate(L) if value > 7)

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to find first index in list i.e. greater than x in python program?

    I have following code for you. Just try to understand this. In this code I have creates one variable which contain various number. After this I have use enumerate(alists) to get greater number.

    Code:
    >>> alist= [0.5, 0.3, 0.9, 0.8]
    >>> [ n for n,i in enumerate(alist) if i>0.7 ][0]
    2

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to find first index in list i.e. greater than x in python program?

    It is very simple to find first index in list i.e. greater than x in python program.
    You have to just use filter function to do this. Look at following code. I have use filter function to get greater number from list "oras".

    Code:
    filter(oras x: x>7, seq)[0]
    But make sure that don't use filter where a list comprehension is both more readable and more per-formant.

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to find first index in list i.e. greater than x in python program?

    Most of the people use enumerate() function to find first index in list i.e. greater than x in python program. It is very easy to use and understand. I have written one example of it. Just try to understand it. I have use for each statement.

    Code:
    for indexs, elems in enumerate(enumbers):
        if elems > references:
            return indexs
    raise ValueError("Nothing Founds")

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    Re: How to find first index in list i.e. greater than x in python program?

    Hey you have to compare each value with others to fin greater value. To do this you have to use xrange() function. Just try to understand following example. In that code I have compare each value of list with other value using seqs[ ] function.


    Code:
    >>> fs=lambdas seqs, m: [ii for ii in xrange(0, len(seq)) if seqs[ii] > n][0]
    >>> fs([5, 3, 9, 8], 7)
    2

Similar Threads

  1. Compiling and Executing a Python program.
    By Blake's in forum Software Development
    Replies: 3
    Last Post: 15-02-2012, 04:46 PM
  2. Unable to run WEI (Windows Experience Index) program
    By UNSTEADY in forum Windows Software
    Replies: 6
    Last Post: 05-09-2011, 09:08 AM
  3. List Index Out Of Bounds 0 In Blue Screen
    By Depo in forum Operating Systems
    Replies: 3
    Last Post: 22-12-2010, 06:10 AM
  4. Replies: 4
    Last Post: 01-05-2010, 01:38 AM
  5. list index out of bounds 4 error
    By John Rodriguez in forum Windows Software
    Replies: 3
    Last Post: 16-06-2009, 09:14 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,554,301.34494 seconds with 17 queries