Weird behavior in search in a list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Su Y

    Weird behavior in search in a list

    hi all,
    I can't understand how this code work, its behavior is really weird
    for me...

    I want find the first number in extend[] which is larger than num, so
    I wrote:
    def find(num):
    count=0
    for elem in extend:
    if elem<num:
    count+=1
    return count

    I found that if extend[] is monotonous, like [1.1, 2.3, 3.2, 4.5,
    5.6],
    it works fine: find(4) returns 3, extend[3] is 4.5.
    But, if extend[] is not monotonous, like [1.1, 2.3, 3.2, 4.5, 5.6,
    4.6, 3.4, 2.1, 0.3],
    find(4) returns 6, extend[6] is 3.4!

    what's going on here? I really can't understand....

  • Su Y

    #2
    Re: Weird behavior in search in a list

    On 3ÔÂ29ÈÕ, ÏÂÎç7ʱ51·Ö, "Su Y" <suyua...@gmail .comwrote:
    hi all,
    I can't understand how this code work, its behavior is really weird
    for me...
    >
    I want find the first number in extend[] which is larger than num, soI wrote:
    >
    def find(num):
    count=0
    for elem in extend:
    if elem<num:
    count+=1
    return count
    >
    I found that if extend[] is monotonous, like [1.1, 2.3, 3.2, 4.5,
    5.6],
    it works fine: find(4) returns 3, extend[3] is 4.5.
    But, if extend[] is not monotonous, like [1.1, 2.3, 3.2, 4.5, 5.6,
    4.6, 3.4, 2.1, 0.3],
    find(4) returns 6, extend[6] is 3.4!
    >
    what's going on here? I really can't understand....
    and I am using Python 2.5 on WinXP.

    Comment

    • Amit Khemka

      #3
      Re: Weird behavior in search in a list

      On 29 Mar 2007 04:51:00 -0700, Su Y <suyuancn@gmail .comwrote:
      hi all,
      I can't understand how this code work, its behavior is really weird
      for me...
      >
      I want find the first number in extend[] which is larger than num, so
      I wrote:
      def find(num):
      count=0
      for elem in extend:
      if elem<num:
      count+=1
      return count
      >
      I found that if extend[] is monotonous, like [1.1, 2.3, 3.2, 4.5,
      5.6],
      it works fine: find(4) returns 3, extend[3] is 4.5.
      But, if extend[] is not monotonous, like [1.1, 2.3, 3.2, 4.5, 5.6,
      4.6, 3.4, 2.1, 0.3],
      find(4) returns 6, extend[6] is 3.4!
      >
      what's going on here? I really can't understand....
      Actually your function "find" returns the number of elements in list
      extend, which are smaller than the argument. Perhaps you wanted to
      'break' when once the condition is met.

      def find(num):
      count=0
      for elem in extend:
      if elem>num: break
      else: count+=1
      return count

      Btw a concise way could be:
      def getfirstbigger( num):
      for i,x in enumerate(exten d):
      if x>num: return i
      return len(extend)

      HTH,


      --
      ----
      Amit Khemka -- onyomo.com
      Home Page: www.cse.iitd.ernet.in/~csd00377
      Endless the world's turn, endless the sun's Spinning, Endless the quest;
      I turn again, back to my own beginning, And here, find rest.

      Comment

      • Amit Khemka

        #4
        Re: Weird behavior in search in a list

        On 29 Mar 2007 04:51:00 -0700, Su Y <suyuancn@gmail .comwrote:
        <snip>
        I want find the first number in extend[] which is larger than num, so
        <snip>
        On 3/29/07, Amit Khemka <khemkaamit@gma il.comwrote:
        Btw a concise way could be:
        def getfirstbigger( num):
        for i,x in enumerate(exten d):
        if x>num: return i
        return len(extend)
        I forgot to add that you can as well 'return' the the item (along with
        the index), but in case all the items in the list are smaller than the
        argument, what return value do you require?
        --
        ----
        Amit Khemka -- onyomo.com
        Home Page: www.cse.iitd.ernet.in/~csd00377
        Endless the world's turn, endless the sun's Spinning, Endless the quest;
        I turn again, back to my own beginning, And here, find rest.

        Comment

        • Josh

          #5
          Re: Weird behavior in search in a list


          "Su Y" <suyuancn@gmail .comwrote in message
          news:1175169306 .021284.46270@e 65g2000hsc.goog legroups.com...
          On 3ÔÂ29ÈÕ, ÏÂÎç7ʱ51·Ö, "Su Y" <suyua...@gmail .comwrote:
          hi all,
          I can't understand how this code work, its behavior is really weird
          for me...
          >
          I want find the first number in extend[] which is larger than num, soI
          wrote:
          >
          def find(num):
          count=0
          for elem in extend:
          if elem<num:
          count+=1
          else:
          break
          return count
          >
          you need to break out of the loop when you first encounter num>elem. The
          reason it works in your sorted list scenario is because elem will be num,
          always, after some point. It won't be in your unsorted list.

          I've added the else: break in your code above
          j


          Comment

          • Michael Bentley

            #6
            Re: Weird behavior in search in a list


            On Mar 29, 2007, at 6:51 AM, Su Y wrote:
            >
            I want find the first number in extend[] which is larger than num, so
            I wrote:
            def find(num):
            count=0
            for elem in extend:
            if elem<num:
            count+=1
            return count
            >
            I found that if extend[] is monotonous, like [1.1, 2.3, 3.2, 4.5,
            5.6],
            it works fine: find(4) returns 3, extend[3] is 4.5.
            But, if extend[] is not monotonous, like [1.1, 2.3, 3.2, 4.5, 5.6,
            4.6, 3.4, 2.1, 0.3],
            find(4) returns 6, extend[6] is 3.4!
            >
            what's going on here? I really can't understand....
            find() loops through the list, and every time it finds a value less
            than num it increments count. So in your second example the values
            1.1, 2.3, 3.2, 3.4, 2.1, and 0.3 are all less than 4, which means
            count will be 6.

            As you learned, a sorted list behaves as you expect. So one approach
            is to sort your list first. But another perhaps better approach is
            to do something like:

            def find(num):
            # check to make sure there *is* a value greater than num
            if max(extend) num:
            # then return the smallest value that is greater than num
            return min([x for x in extend if x num])
            else:
            return None

            Hope this helps,
            Michael

            Comment

            • Amit Khemka

              #7
              Re: Weird behavior in search in a list

              On 3/29/07, Michael Bentley <michael@jedimi ndworks.comwrot e:
              >
              On Mar 29, 2007, at 6:51 AM, Su Y wrote:

              I want find the first number in extend[] which is larger than num, so
              <snip>
              def find(num):
              # check to make sure there *is* a value greater than num
              if max(extend) num:
              # then return the smallest value that is greater than num
              return min([x for x in extend if x num])
              else:
              return None
              I think OP wants the first value in the list greater than 'num' not
              the smallest greater value in the list.

              cheers,
              --
              ----
              Amit Khemka -- onyomo.com
              Home Page: www.cse.iitd.ernet.in/~csd00377
              Endless the world's turn, endless the sun's Spinning, Endless the quest;
              I turn again, back to my own beginning, And here, find rest.

              Comment

              • Su Y

                #8
                Re: Weird behavior in search in a list

                On 3ÔÂ29ÈÕ, ÏÂÎç8ʱ22·Ö, Michael Bentley <mich...@jedimi ndworks.comwrot e:
                On Mar 29, 2007, at 6:51 AM, Su Y wrote:
                >
                >
                >
                >
                >
                I want find the first number in extend[] which is larger than num, so
                I wrote:
                def find(num):
                count=0
                for elem in extend:
                if elem<num:
                count+=1
                return count
                >
                I found that if extend[] is monotonous, like [1.1, 2.3, 3.2, 4.5,
                5.6],
                it works fine: find(4) returns 3, extend[3] is 4.5.
                But, if extend[] is not monotonous, like [1.1, 2.3, 3.2, 4.5, 5.6,
                4.6, 3.4, 2.1, 0.3],
                find(4) returns 6, extend[6] is 3.4!
                >
                what's going on here? I really can't understand....
                >
                find() loops through the list, and every time it finds a value less
                than num it increments count. So in your second example the values
                1.1, 2.3, 3.2, 3.4, 2.1, and 0.3 are all less than 4, which means
                count will be 6.
                >
                As you learned, a sorted list behaves as you expect. So one approach
                is to sort your list first. But another perhaps better approach is
                to do something like:
                >
                def find(num):
                # check to make sure there *is* a value greater than num
                if max(extend) num:
                # then return the smallest value that is greater than num
                return min([x for x in extend if x num])
                else:
                return None
                >
                Hope this helps,
                Michael
                oh yes, it's the "break" I forgot... Thank you, it helps me a lot!

                Comment

                • Hendrik van Rooyen

                  #9
                  Re: Weird behavior in search in a list

                  "Su Y" <s...ncn@gmail. comwrote:
                  I want find the first number in extend[] which is larger than num, so
                  I wrote:
                  def find(num):
                  count=0
                  for elem in extend:
                  if elem<num:
                  count+=1
                  return count
                  >
                  I found that if extend[] is monotonous, like [1.1, 2.3, 3.2, 4.5,
                  5.6],
                  it works fine: find(4) returns 3, extend[3] is 4.5.
                  But, if extend[] is not monotonous, like [1.1, 2.3, 3.2, 4.5, 5.6,
                  4.6, 3.4, 2.1, 0.3],
                  find(4) returns 6, extend[6] is 3.4!
                  >
                  what's going on here? I really can't understand....
                  Hint: extend[0] is1.1
                  Hint: extend[7] is 2.1
                  Hint: 2.1 is less than 4

                  You have to stop counting and come out of the loop when you find the
                  first one - what your function is doing is counting the elements less than
                  num, not finding the first one that is.

                  hth - Hendrik


                  Comment

                  Working...