a_list.count(a_callable) ?

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

    #1

    a_list.count(a_callable) ?

    Hi,

    I'm wondering if it is useful to extend the count() method of a list
    to accept a callable object? What it does should be quite intuitive:
    count the number of items that the callable returns True or anything
    logically equivalent (non-empty sequence, non-zero number, etc).

    This would return the same result as len(filter(a_ca llable, a_list)),
    but without constructing an intermediate list which is thrown away
    after len() is done.

    This would also be equivalent to
    n = 0
    for i in a_list:
    if a_callable(i): n += 1
    but with much shorter and easier-to-read code. It would also run
    faster.

    This is my first post and please bear with me if I'm not posting it in
    the right way.

    Regards,
    Ping

  • Dustan

    #2
    Re: a_list.count(a_ callable) ?

    On Jun 14, 2:53 pm, Ping <ping.nsr....@g mail.comwrote:
    Hi,
    >
    I'm wondering if it is useful to extend the count() method of a list
    to accept a callable object? What it does should be quite intuitive:
    count the number of items that the callable returns True or anything
    logically equivalent (non-empty sequence, non-zero number, etc).
    >
    This would return the same result as len(filter(a_ca llable, a_list)),
    map and filter are basically obsolete after the introduction of list
    comprehensions; your expression is equivalent to:
    len([i for i in a_list if a_callable(i)])

    Which can then be converted into a generator expression (round
    brackets instead of square brackets) to avoid the intermediate list:
    len((i for i in a_list if a_callable(i)))

    Or syntactically equivalent (avoiding lispy brackets):
    len(i for i in a_list if a_callable(i))
    but without constructing an intermediate list which is thrown away
    after len() is done.
    >
    This would also be equivalent to
    n = 0
    for i in a_list:
    if a_callable(i): n += 1
    but with much shorter and easier-to-read code. It would also run
    faster.
    >
    This is my first post and please bear with me if I'm not posting it in
    the right way.
    >
    Regards,
    Ping

    Comment

    • Dustan

      #3
      Re: a_list.count(a_ callable) ?

      On Jun 14, 3:37 pm, Dustan <DustanGro...@g mail.comwrote:
      map and filter are basically obsolete after the introduction of list
      comprehensions
      It is probably worth noting that list comprehensions do not require
      that you write a new function; they take any expression where
      appropriate. For more information on list comprehensions, see:



      Generator expressions are the same, except syntactically they have
      round brackets instead of square, and they return a generator instead
      of a list, which allows for lazy evaluation.

      Comment

      • Dustan

        #4
        Re: a_list.count(a_ callable) ?

        On Jun 14, 3:37 pm, Dustan <DustanGro...@g mail.comwrote:
        Which can then be converted into a generator expression (round
        brackets instead of square brackets) to avoid the intermediate list:
        len((i for i in a_list if a_callable(i)))
        Sorry for the excess of posts everybody.

        I just realized that the generator expression would not work. I'm not
        sure how else could be implemented efficiently and without using up
        memory besides by accumulating the count as your earlier example shows.

        Comment

        • Carsten Haese

          #5
          Re: a_list.count(a_ callable) ?

          On Thu, 2007-06-14 at 21:06 +0000, Dustan wrote:
          On Jun 14, 3:37 pm, Dustan <DustanGro...@g mail.comwrote:
          Which can then be converted into a generator expression (round
          brackets instead of square brackets) to avoid the intermediate list:
          len((i for i in a_list if a_callable(i)))
          >
          Sorry for the excess of posts everybody.
          >
          I just realized that the generator expression would not work. I'm not
          sure how else could be implemented efficiently and without using up
          memory besides by accumulating the count as your earlier example shows.
          sum(1 for i in a_list if a_callable(i))

          --
          Carsten Haese



          Comment

          • Carsten Haese

            #6
            Re: a_list.count(a_ callable) ?

            On Thu, 2007-06-14 at 12:53 -0700, Ping wrote:
            Hi,
            >
            I'm wondering if it is useful to extend the count() method of a list
            to accept a callable object? What it does should be quite intuitive:
            count the number of items that the callable returns True or anything
            logically equivalent (non-empty sequence, non-zero number, etc).
            >
            This would return the same result as len(filter(a_ca llable, a_list)),
            but without constructing an intermediate list which is thrown away
            after len() is done.
            >
            This would also be equivalent to
            n = 0
            for i in a_list:
            if a_callable(i): n += 1
            but with much shorter and easier-to-read code. It would also run
            faster.
            As an alternative to the generator-sum approach I posted in reply to
            Dustan's suggestion, you could (ab)use the fact that count() uses
            equality testing and do something like this:
            >>class Oddness(object) :
            .... def __eq__(self, other): return other%2==1
            ....
            >>[1,2,3,4,5].count(Oddness( ))
            3

            I don't know which approach is faster. Feel free to compare the two.

            HTH,

            --
            Carsten Haese



            Comment

            • Ping

              #7
              Re: a_list.count(a_ callable) ?

              >
              sum(1 for i in a_list if a_callable(i))
              >
              --
              Carsten Haesehttp://informixdb.sour ceforge.net
              This works nicely but not very intuitive or readable to me.

              First of all, the generator expression makes sense only to
              trained eyes. Secondly, using sum(1 ...) to mean count()
              isn't very intuitive either.

              I would still prefer an expression like a_list.count(a_ callable),
              which is short, clean, and easy to understand. :) However,
              it does produce ambiguities if a_list is a list of callables.
              Should the count() method match values or check return values
              of a_callable? There are several possible designs but I'm not
              sure which is better.

              Ping

              Comment

              • Dustan

                #8
                Re: a_list.count(a_ callable) ?

                On Jun 15, 9:15 am, Ping <ping.nsr....@g mail.comwrote:
                sum(1 for i in a_list if a_callable(i))
                >
                --
                Carsten Haesehttp://informixdb.sour ceforge.net
                >
                This works nicely but not very intuitive or readable to me.
                >
                First of all, the generator expression makes sense only to
                trained eyes. Secondly, using sum(1 ...) to mean count()
                isn't very intuitive either.
                Then wrap it in a function:
                def count(a_list, a_function):
                return sum(1 for i in a_list if a_function(i))

                And call the function. You can also give it a different name (although
                I can't think of a concise name that would express it any better).
                I would still prefer an expression like a_list.count(a_ callable),
                which is short, clean, and easy to understand. :) However,
                it does produce ambiguities if a_list is a list of callables.
                Should the count() method match values or check return values
                of a_callable? There are several possible designs but I'm not
                sure which is better.
                Indeed, the ambiguity in that situation would be a reason *not* to
                introduce such behavior, especially since it would break older
                programs that don't recognize that behavior. Just stick with writing
                the function, as shown above.

                Comment

                • Carsten Haese

                  #9
                  Re: a_list.count(a_ callable) ?

                  On Fri, 2007-06-15 at 14:15 +0000, Ping wrote:
                  using sum(1 ...) to mean count() isn't very intuitive
                  I find it very intuitive, but then again, my years of studying Math may
                  have skewed my intuition.
                  I would still prefer an expression like a_list.count(a_ callable),
                  which is short, clean, and easy to understand.
                  Did you see my alternative example on this thread? It allows you to use
                  list.count in almost exactly that way, except that instead of passing
                  the callable directly, you pass an object that defers to your callable
                  in its __eq__ method.

                  HTH,

                  --
                  Carsten Haese



                  Comment

                  • Ping

                    #10
                    Re: a_list.count(a_ callable) ?

                    On 6 15 , 11 17 , Dustan <DustanGro...@g mail.comwrote:
                    On Jun 15, 9:15 am, Ping <ping.nsr....@g mail.comwrote:
                    >
                    sum(1 for i in a_list if a_callable(i))
                    >
                    --
                    Carsten Haesehttp://informixdb.sour ceforge.net
                    >
                    This works nicely but not very intuitive or readable to me.
                    >
                    First of all, the generator expression makes sense only to
                    trained eyes. Secondly, using sum(1 ...) to mean count()
                    isn't very intuitive either.
                    >
                    Then wrap it in a function:
                    def count(a_list, a_function):
                    return sum(1 for i in a_list if a_function(i))
                    >
                    And call the function. You can also give it a different name (although
                    I can't think of a concise name that would express it any better).
                    >
                    Hmm... This sounds like the best idea so far. It is efficient both
                    in memory and time while exposes an easy-to-understand name.
                    I would name the function count_items though.

                    n = count_items(a_l ist, lambda x: x 3) # very readable :)

                    cheers,
                    Ping

                    Comment

                    • Ping

                      #11
                      Re: a_list.count(a_ callable) ?

                      On 6 16 , 12 33 , Carsten Haese <cars...@uniqsy s.comwrote:
                      On Fri, 2007-06-15 at 14:15 +0000, Ping wrote:
                      using sum(1 ...) to mean count() isn't very intuitive
                      >
                      I find it very intuitive, but then again, my years of studying Math may
                      have skewed my intuition.
                      >
                      I would still prefer an expression like a_list.count(a_ callable),
                      which is short, clean, and easy to understand.
                      >
                      Did you see my alternative example on this thread? It allows you to use
                      list.count in almost exactly that way, except that instead of passing
                      the callable directly, you pass an object that defers to your callable
                      in its __eq__ method.
                      >
                      HTH,
                      >
                      --
                      Carsten Haesehttp://informixdb.sour ceforge.net
                      Yes, I read it. This works, but it may make lots of dummy classes
                      with the special __eq__ method if I have many criteria to use for
                      counting. I find the function design mentioned by Dustan most
                      attractive. :)

                      cheers,
                      Ping

                      Comment

                      • =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

                        #12
                        Re: a_list.count(a_ callable) ?

                        On 6/15/07, Ping <ping.nsr.yeh@g mail.comwrote:

                        sum(1 for i in a_list if a_callable(i))

                        --
                        Carsten Haesehttp://informixdb.sour ceforge.net
                        >
                        This works nicely but not very intuitive or readable to me.
                        >
                        First of all, the generator expression makes sense only to
                        trained eyes. Secondly, using sum(1 ...) to mean count()
                        isn't very intuitive either.
                        >
                        I would still prefer an expression like a_list.count(a_ callable),
                        which is short, clean, and easy to understand. :) However,
                        it does produce ambiguities if a_list is a list of callables.
                        Should the count() method match values or check return values
                        of a_callable? There are several possible designs but I'm not
                        sure which is better.
                        Maybe you could extend count() analogous to how sort() works:

                        # L is a list of Person objects, each Person has a name attribute
                        L.sort(key = attrgetter("nam e"))

                        # How many olle are there?
                        print L.count("olle", key = attrgetter("nam e"))

                        # And index could be extended in the same way!
                        # Whom has id 1234?
                        print L.index(1234, key = attrgetter("id" )).name

                        All of these could be solved by giving Person an __eq__() method, but
                        it fails when you need to search, sort or count on a different key.

                        --
                        mvh Björn

                        Comment

                        • Carsten Haese

                          #13
                          Re: a_list.count(a_ callable) ?

                          On Fri, 2007-06-15 at 17:55 +0000, Ping wrote:
                          On 6 16 , 12 33 , Carsten Haese <cars...@uniqsy s.comwrote:
                          Did you see my alternative example on this thread? It allows you to use
                          list.count in almost exactly that way, except that instead of passing
                          the callable directly, you pass an object that defers to your callable
                          in its __eq__ method.

                          HTH,

                          --
                          Carsten Haesehttp://informixdb.sour ceforge.net
                          >
                          Yes, I read it. This works, but it may make lots of dummy classes
                          with the special __eq__ method if I have many criteria to use for
                          counting.
                          No, it won't. You only need one class that is given the criterion at
                          instantiation time. Something like this:

                          class WhereTrue(objec t):
                          def __init__(self, func):
                          self.func = func
                          def __eq__(self, other):
                          return self.func(other )

                          list1.count(Whe reTrue(callable 1))
                          list2.count(Whe reTrue(callable 2))

                          HTH,

                          --
                          Carsten Haese



                          Comment

                          • Carsten Haese

                            #14
                            Re: a_list.count(a_ callable) ?

                            On Fri, 2007-06-15 at 14:39 -0400, Carsten Haese wrote:
                            class WhereTrue(objec t):
                            def __init__(self, func):
                            self.func = func
                            def __eq__(self, other):
                            return self.func(other )
                            >
                            list1.count(Whe reTrue(callable 1))
                            list2.count(Whe reTrue(callable 2))
                            P.S: Note, however, that this will only work if the list elements don't
                            define __eq__ methods themselves. If they do, their __eq__ methods get
                            called instead of WhereTrue's __eq__ method, leading to incorrect
                            results.

                            --
                            Carsten Haese



                            Comment

                            • Dustan

                              #15
                              Re: a_list.count(a_ callable) ?

                              On Jun 15, 12:52 pm, Ping <ping.nsr....@g mail.comwrote:
                              On 6 15 , 11 17 , Dustan <DustanGro...@g mail.comwrote:
                              >
                              >
                              >
                              On Jun 15, 9:15 am, Ping <ping.nsr....@g mail.comwrote:
                              >
                              sum(1 for i in a_list if a_callable(i))
                              >
                              --
                              Carsten Haesehttp://informixdb.sour ceforge.net
                              >
                              This works nicely but not very intuitive or readable to me.
                              >
                              First of all, the generator expression makes sense only to
                              trained eyes. Secondly, using sum(1 ...) to mean count()
                              isn't very intuitive either.
                              >
                              Then wrap it in a function:
                              def count(a_list, a_function):
                              return sum(1 for i in a_list if a_function(i))
                              >
                              And call the function. You can also give it a different name (although
                              I can't think of a concise name that would express it any better).
                              >
                              Hmm... This sounds like the best idea so far. It is efficient both
                              in memory and time while exposes an easy-to-understand name.
                              I would name the function count_items though.
                              >
                              n = count_items(a_l ist, lambda x: x 3) # very readable :)
                              Although that particular example would be more efficient inlined,
                              because of the additional time spent creating the lambda function in
                              your count_items call.

                              sum(1 for i in a_list if i 3)

                              Comment

                              Working...