Results 1 to 10 of 10

Thread: Python greatest number in the list

  1. #1
    Join Date
    Jul 2013
    Beans
    1

    Python greatest number in the list

    solved!
    Last edited by mishumishu; July 9th, 2013 at 07:40 AM.

  2. #2
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Python greatest number in the list

    Well, we are all very happy for you but what was the question?

  3. #3
    Join Date
    Apr 2013
    Location
    43.49°N 7.46°E
    Beans
    117
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Python greatest number in the list

    Quote Originally Posted by ofnuts View Post
    Well, we are all very happy for you but what was the question?
    let's try to guess the question and the answer:
    the question:
    given a list of numbers, how can I find the greatest of them in Python?
    the answer:
    use
    Code:
    max(your_list_of_numbers)

  4. #4
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Python greatest number in the list

    Besides the one good answer, there are several not-so-good ones (like sorted(list)[-1])

  5. #5
    Join Date
    Apr 2013
    Location
    43.49°N 7.46°E
    Beans
    117
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Python greatest number in the list

    Quote Originally Posted by ofnuts View Post
    Code:
    sorted(list)[-1]
    this is a sample of a slightly obfuscated code, isn't it? Indeed, at least for me, the purpose of a code like this isn't at all easy to understand.

  6. #6

    Re: Python greatest number in the list

    Seems perfectly clear to me: take the last element of a sorted list.

  7. #7
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Python greatest number in the list

    Next time I'll use 'reduce(lambda x,y : x if x >y else y,list)'

  8. #8
    Join Date
    Apr 2013
    Location
    43.49°N 7.46°E
    Beans
    117
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Python greatest number in the list

    what I meant to say in my previous post is: "sorting a list hasn't always the purpose of finding its maximum value; why should I use sorted(list[-1]) when it's much more understandable max(list)?"

  9. #9
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Python greatest number in the list

    My original question was really wether the OP found the right solution (max(list)), or stumbled on something else that does the job but isn't 100% correct (like the ones I mentioned). Since he didn't tell us, we don't know.

  10. #10
    Join Date
    Jun 2009
    Location
    Land of Paranoia and Guns
    Beans
    194
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Python greatest number in the list

    This is why knowing the correct answer to questions like this is important:
    Code:
    l = list(range(1000)); random.shuffle(l)
    max(l): 0.105857849121 milliseconds
    sorted(l)[-1]: 0.613212585449 milliseconds
    reduce(lambda x, y : x if x > y else y, l): 1.07598304749 milliseconds
    Time differences like these stack up very quickly in business applications that handle hundreds or even thousands of queries per second. It is important to know the fastest way to handle trivial operations like this so you don't drive yourself nuts when trying to optimize your code later.
    Don't use W3Schools as a resource! (Inconsequential foul language at the jump)
    Open Linux Forums (More foul language, but well worth it for the quality of support and good humor.)
    If you want to discuss W3Schools, please PM me instead of posting.

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
  •