any() and all() on empty list?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carl Banks

    #46
    Re: any() and all() on empty list?


    Ron Adam wrote:[color=blue]
    > Carl Banks wrote:
    >[color=green]
    > > In Python, yes and no are the only possible answers. Probably the only
    > > analogous thing you could do in Python would be for all() to raise
    > > ValueError when passed an empty sequence.[/color]
    >
    > There is also 'None' which serves a similar purpose of indicating an
    > invalid value when passing arguments.[/color]

    If all() were to return None, then if would essentially be like
    returning False, because an if-statement would treat False and None the
    same (as would most anything else expecting a boolean value).

    The only reasonable way to say "false assumption" in Python is to raise
    an exception.


    Carl Banks

    Comment

    • Ron Adam

      #47
      Re: any() and all() on empty list?

      Carl Banks wrote:[color=blue]
      > Ron Adam wrote:[color=green]
      >> Carl Banks wrote:
      >>[color=darkred]
      >>> In Python, yes and no are the only possible answers. Probably the only
      >>> analogous thing you could do in Python would be for all() to raise
      >>> ValueError when passed an empty sequence.[/color]
      >> There is also 'None' which serves a similar purpose of indicating an
      >> invalid value when passing arguments.[/color]
      >
      > If all() were to return None, then if would essentially be like
      > returning False, because an if-statement would treat False and None the
      > same (as would most anything else expecting a boolean value).
      >
      > The only reasonable way to say "false assumption" in Python is to raise
      > an exception.
      >
      >
      > Carl Banks[/color]

      Then maybe None should be evaluated as True so it is consistent with
      all(). ;-)


      Not serious of course, Cheers,
      Ron







      Comment

      • Ant

        #48
        Re: any() and all() on empty list?

        lol!

        Comment

        • Paul Rubin

          #49
          Re: any() and all() on empty list?

          Ron Adam <rrr@ronadam.co m> writes:[color=blue]
          > The 'not not S' is just a conversion to bool. Is the following less
          > contorted to you?
          >[color=green][color=darkred]
          > >>> bool([])[/color][/color]
          > False[/color]

          Oh ok. Yes, bool(S) is much less contorted than "not not S".
          [color=blue]
          > 'Is all True' isn't the same as 'Has all True'. As I said, I'm not
          > questioning the mathematical meaning of the set relation 'is all
          > True', but wondering weather or not an alternate relation 'has all
          > True' would be better for use as a flow control test.
          >
          > Do you have some examples uses since it's obvious to you?[/color]

          # go out drinking when I'm finished with today's work
          if all (task.done() for task in self.things_to_ do_today()):
          self.go_out_dri nking()

          If I didn't have anything to do today, that should result in going out
          drinking immediately.
          [color=blue]
          > I just have a feeling we will see a lot of "S and all(S)" expressions
          > being used. Maybe that's not so bad, but I would prefer to not have
          > to do that if it turns out to the standard idiom for all testing
          > within a loop.[/color]

          I think "S and all(S)" is the right way to express that, if that's
          what's intended.

          Comment

          • Steve R. Hastings

            #50
            Re: any() and all() on empty list?

            On Fri, 31 Mar 2006 16:29:00 -0800, Paul Rubin wrote:[color=blue]
            > I think "S and all(S)" is the right way to express that, if that's
            > what's intended.[/color]

            I still would like a standard function, because "S and all(S)" does not
            work with iterators. I proposed one possible function, truecount(S), that
            returns a tuple of how many were true and how many there were total. Then
            you could do

            true_count, count = truecount(S)

            if count and true_count == count:
            # nonempty list and all are true


            And S could be an iterator or generator function expression.

            You can easily write your own truecount() but it would be nice to have
            something like that as standard. I don't much like the name "truecount"
            though; I'm open to suggestions for a better name.
            --
            Steve R. Hastings "Vita est"
            steve@hastings. org http://www.blarg.net/~steveha

            Comment

            • Ron Adam

              #51
              Re: any() and all() on empty list?

              Steve R. Hastings wrote:[color=blue]
              > On Fri, 31 Mar 2006 16:29:00 -0800, Paul Rubin wrote:[color=green]
              >> I think "S and all(S)" is the right way to express that, if that's
              >> what's intended.[/color]
              >
              > I still would like a standard function, because "S and all(S)" does not
              > work with iterators. I proposed one possible function, truecount(S), that
              > returns a tuple of how many were true and how many there were total. Then
              > you could do
              >
              > true_count, count = truecount(S)
              >
              > if count and true_count == count:
              > # nonempty list and all are true
              >
              >
              > And S could be an iterator or generator function expression.
              >
              > You can easily write your own truecount() but it would be nice to have
              > something like that as standard. I don't much like the name "truecount"
              > though; I'm open to suggestions for a better name.[/color]

              How about:

              countall(S, value=True)


              Considering len() is used to get a length, and countall() is related to
              all(), but it's explicit about what it's counting and would not return
              True on an empty set. I think it would be useful.

              true_count, count = countall(S), len(S)

              Cheers,
              Ron

              Comment

              • Paul Rubin

                #52
                Re: any() and all() on empty list?

                Ron Adam <rrr@ronadam.co m> writes:[color=blue]
                > true_count, count = countall(S), len(S)[/color]

                In general it's preferable to not rely on len being available, since
                these are arbitrary iterators.

                Comment

                • Steve R. Hastings

                  #53
                  Re: any() and all() on empty list?

                  On Sat, 01 Apr 2006 02:06:29 -0600, Ron Adam wrote:[color=blue]
                  > true_count, count = countall(S), len(S)[/color]

                  Unfortunately, this does not solve the problem.

                  An iterator yields its values only once. If you have an iterator "itr",
                  you cannot do all(itr) and then do len(itr). Whichever one you do first
                  will run the iterator to exhaustion.

                  This is why my proposed truecount() returns a tuple, with the length and
                  the count of true values.

                  Suppose you wanted, for some reason, to know how many lines in a file
                  start with a vowel:

                  vowels = frozenset("aeio uAEIOU")
                  f = open("a_file.tx t") # note that f is an iterator
                  true_count, count = truecount(line[0] in vowels for line in f)
                  print "%d lines in file; %d start with a vowel" % (count, true_count)


                  Because f is an iterator, we only get one pass through the values of the
                  file. Because truecount() returns a tuple with two values, one pass is
                  enough.
                  --
                  Steve R. Hastings "Vita est"
                  steve@hastings. org http://www.blarg.net/~steveha

                  Comment

                  • Steve R. Hastings

                    #54
                    Re: any() and all() on empty list?

                    On Sat, 01 Apr 2006 00:38:08 -0800, Steve R. Hastings wrote:[color=blue]
                    > my proposed truecount() returns a tuple, with the length and
                    > the count of true values.[/color]

                    I never liked the name truecount(), and the more I think about it, the
                    less I like the function. It should either solve a very important need,
                    or else it should be general enough to solve multiple needs; truecount()
                    doesn't really do either.

                    I kept thinking about this, and then suddenly I remembered an idea I read
                    about before. I'm not sure who invented this idea, but it wasn't me.

                    Here is a function called "tally()". It reduces a list to a dictionary
                    of counts, with one key for each value from the list. The value for
                    each key is the count of how many times it appeared in the list.


                    def tally(seq, d=None):
                    if d == None:
                    d = {}

                    for x in seq:
                    if x in d:
                    d[x] += 1
                    else:
                    d[x] = 1
                    return d



                    This neatly replaces truecount(), and you can use it for other things as
                    well.

                    Let's revisit my example from before:
                    [color=blue]
                    > Suppose you wanted, for some reason, to know how many lines in a file
                    > start with a vowel:[/color]


                    vowels = frozenset("aeio uAEIOU")
                    f = open("a_file.tx t") # note that f is an iterator

                    counts = tally(line[0] in vowels for line in f)
                    # counts is a dict; counts.keys() == [False, True]
                    count_nonvowels , count_vowels = counts.values()

                    total = count_nonvowels + count_vowels
                    print "%d lines in file; %d start with a vowel" % (total, count_vowels)

                    --
                    Steve R. Hastings "Vita est"
                    steve@hastings. org http://www.blarg.net/~steveha

                    Comment

                    • Felipe Almeida Lessa

                      #55
                      Re: any() and all() on empty list?

                      Em Sáb, 2006-04-01 às 08:35 -0800, Steve R. Hastings escreveu:[color=blue]
                      > def tally(seq, d=None):
                      > if d == None:
                      > d = {}
                      >
                      > for x in seq:
                      > if x in d:
                      > d[x] += 1
                      > else:
                      > d[x] = 1
                      > return d[/color]

                      Two changes:
                      - Use "is None".
                      - Use "try ... except" instead of "in"

                      def tally2(seq, d=None):
                      if d is None:
                      d = {}
                      for x in seq:
                      try:
                      d[x] += 1
                      except KeyError:
                      d[x] = 1
                      return d

                      It's also faster:
                      [color=blue][color=green][color=darkred]
                      >>> from random import choice
                      >>> a = []
                      >>> for i in xrange(100000):[/color][/color][/color]
                      .... a.append(choice ([False, True]))
                      ....[color=blue][color=green][color=darkred]
                      >>> tally(a)[/color][/color][/color]
                      {False: 49922, True: 50078}[color=blue][color=green][color=darkred]
                      >>> tally2(a)[/color][/color][/color]
                      {False: 49922, True: 50078}[color=blue][color=green][color=darkred]
                      >>> from timeit import Timer
                      >>> min(Timer(stmt= 'b=tally(a)', setup='from __main__ import a,[/color][/color][/color]
                      tally').repeat( 3, 100))
                      4.2756481170654 297[color=blue][color=green][color=darkred]
                      >>> min(Timer(stmt= 'b=tally2(a)', setup='from __main__ import a,[/color][/color][/color]
                      tally2').repeat (3, 100))
                      3.8120281696319 58

                      Maybe you don't like my version, and the gains aren't that much, but
                      please use "is None" instead of "== None".

                      Cheers,

                      --
                      Felipe.

                      Comment

                      • Steve R. Hastings

                        #56
                        Re: any() and all() on empty list?

                        On Sat, 01 Apr 2006 14:35:58 -0300, Felipe Almeida Lessa wrote:[color=blue]
                        > Two changes:
                        > - Use "is None".
                        > - Use "try ... except" instead of "in"[/color]

                        Yes.

                        [color=blue]
                        > Maybe you don't like my version, and the gains aren't that much, but
                        > please use "is None" instead of "== None".[/color]

                        Actually, I do like your version. And I try to always use "is None"
                        instead of "== None"; today I made a mistake about it. Thank you for your
                        comments.

                        Ideally there should be an official tally() function in some module in
                        Python, and then we can just use it and not worry about how to write it. :-)
                        --
                        Steve R. Hastings "Vita est"
                        steve@hastings. org http://www.blarg.net/~steveha

                        Comment

                        • Ron Adam

                          #57
                          Re: any() and all() on empty list?

                          Steve R. Hastings wrote:
                          [color=blue]
                          > Here is a function called "tally()". It reduces a list to a dictionary
                          > of counts, with one key for each value from the list. The value for
                          > each key is the count of how many times it appeared in the list.
                          >
                          >
                          > def tally(seq, d=None):
                          > if d == None:
                          > d = {}
                          >
                          > for x in seq:
                          > if x in d:
                          > d[x] += 1
                          > else:
                          > d[x] = 1
                          > return d
                          >
                          >
                          > This neatly replaces truecount(), and you can use it for other things as
                          > well.[/color]

                          if True in talley(S): do_somthing()

                          Works for me... ;-)


                          Cheers,
                          Ron




                          Comment

                          • Ron Adam

                            #58
                            Re: any() and all() on empty list?

                            Steve R. Hastings wrote:[color=blue]
                            > On Sat, 01 Apr 2006 14:35:58 -0300, Felipe Almeida Lessa wrote:[color=green]
                            >> Two changes:
                            >> - Use "is None".
                            >> - Use "try ... except" instead of "in"[/color]
                            >
                            > Yes.
                            >
                            >[color=green]
                            >> Maybe you don't like my version, and the gains aren't that much, but
                            >> please use "is None" instead of "== None".[/color]
                            >
                            > Actually, I do like your version. And I try to always use "is None"
                            > instead of "== None"; today I made a mistake about it. Thank you for your
                            > comments.
                            >
                            > Ideally there should be an official tally() function in some module in
                            > Python, and then we can just use it and not worry about how to write it. :-)[/color]

                            And it's a good candidate to be written in C as well.

                            Cheers,
                            Ron

                            Comment

                            • Ron Adam

                              #59
                              Re: any() and all() on empty list?

                              Ron Adam wrote:[color=blue]
                              > Steve R. Hastings wrote:[/color]
                              [color=blue][color=green]
                              >> This neatly replaces truecount(), and you can use it for other things as
                              >> well.[/color]
                              >
                              > if True in talley(S): do_somthing()
                              >
                              > Works for me... ;-)
                              >
                              >
                              > Cheers,
                              > Ron[/color]

                              Actulley talley isn't needed for this...

                              if True in S: do_domething()


                              That's what I get for staying up way too late last night.

                              Cheers, Ron

                              Comment

                              • Piet van Oostrum

                                #60
                                Re: any() and all() on empty list?

                                >>>>> "Steve R. Hastings" <steve@hastings .org> (SRH) wrote:
                                [snip][color=blue]
                                >SRH> vowels = frozenset("aeio uAEIOU")
                                >SRH> f = open("a_file.tx t") # note that f is an iterator[/color]
                                [color=blue]
                                >SRH> counts = tally(line[0] in vowels for line in f)[/color]

                                tally([line[0] in vowels for line in f])
                                [color=blue]
                                >SRH> # counts is a dict; counts.keys() == [False, True][/color]

                                No guarantee about the order. It could be [True, False].
                                [color=blue]
                                >SRH> count_nonvowels , count_vowels = counts.values()[/color]

                                So you must use counts[False] and counts[True].

                                --
                                Piet van Oostrum <piet@cs.uu.n l>
                                URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C 4]
                                Private email: piet@vanoostrum .org

                                Comment

                                Working...