simultaneous assignment

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

    #46
    Re: simultaneous assignment

    bruno at modulix wrote:[color=blue]
    > John Salerno wrote:[color=green]
    >> Bruno Desthuilliers wrote:
    >>[color=darkred]
    >>> But my question (sorry, it may not have been clear) was more along the
    >>> line of : "why do you worry about identity in the given snippet ?".[/color]
    >>
    >> Actually, I kind of thought that maybe it *didn't* matter in this
    >> particular example anyway, so my question was meant to be more general
    >> than I might have written it. It seems like the identity issue can be a
    >> problem in certain cases, although I can't think of a case right now! :)[/color]
    >
    > something
    > a = b = []
    > a.append(1)
    > print b
    >
    >[/color]

    Ah ha!

    Comment

    • Edward Elliott

      #47
      Re: simultaneous assignment

      Steve R. Hastings wrote:[color=blue]
      > You could also use a function that counts all different values in a list,
      > reducing the list to a dictionary whose keys are the unique values from
      > the list.[/color]

      Wouldn't reducing to a set instead of a dict make more sense if all you want
      to do is count uniq elements?

      Comment

      • Steve R. Hastings

        #48
        Re: simultaneous assignment

        On Wed, 03 May 2006 17:51:03 +0000, Edward Elliott wrote:
        [color=blue]
        > Steve R. Hastings wrote:[color=green]
        >> You could also use a function that counts all different values in a list,
        >> reducing the list to a dictionary whose keys are the unique values from
        >> the list.[/color]
        >
        > Wouldn't reducing to a set instead of a dict make more sense if all you want
        > to do is count uniq elements?[/color]

        My apologies for not explaining tally() better.

        The dict has one key for each unique element, and the value associated
        with each key is a count of how many times that element appeared in the
        original list.

        lst = ['a', 'b', 'b', 'c', 'c', 'c']
        d = iterwrap.tally( lst)
        print d # prints something like: {'a': 1, 'c': 3, 'b': 2}


        If you didn't care how many times the values appeared in the original
        list, and just just wanted the unique values, then a set would be perfect.

        If you happen to have tally(), it is an easy way to solve the original
        problem: figure out whether a list has exactly one true value in it.

        d = tally(bool(x) for x in lst)
        if d[True] == 1:
        print "and there was much rejoicing"

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

        Comment

        • Dave Hansen

          #49
          Re: simultaneous assignment

          On Tue, 02 May 2006 18:52:48 GMT in comp.lang.pytho n, John Salerno
          <johnjsal@NOSPA Mgmail.com> wrote:

          [...][color=blue]
          >
          >Yeah, after trying some crazy things, I just wrote it this way:
          >
          >def truth_test(seq) :
          > truth = 0
          > for item in seq:
          > if item:
          > truth += 1
          > if truth == 1:
          > return True
          > else:
          > return False[/color]

          You could replace those last four lines with

          return truth == 1
          [color=blue]
          >
          >Not sure I like having to keep a counter though, but the other stuff I[/color]

          Well, if you want something minimalist, you could try

          def truth_test(seq) :
          return sum(1 for item in seq if item) == 1

          Though I'm not sure it's really any clearer...
          [color=blue]
          >did was really convoluted, like checking to see if the first item was
          >True, and if it was, popping it from the list and iterating over the
          >rest of the items (needless to say, the in-place change wasn't helpful).[/color]

          Perhaps something like

          def truth_test(seq) :
          found = False
          for item in seq:
          if item:
          if found:
          return False
          found = True
          return found

          Gets you an early exit, anyway...

          All code untested. Regards,
          -=Dave

          --
          Change is inevitable, progress is not.

          Comment

          • Paul Rubin

            #50
            Re: simultaneous assignment

            Dave Hansen <iddw@hotmail.c om> writes:[color=blue]
            > Well, if you want something minimalist, you could try
            >
            > def truth_test(seq) :
            > return sum(1 for item in seq if item) == 1[/color]

            def truth_test(seq) :
            return sum(map(bool, seq)) == 1

            Comment

            • Boris Borcic

              #51
              Re: simultaneous assignment

              Steve R. Hastings wrote:
              [color=blue]
              > You could also use a function that counts all different values in a list,
              > reducing the list to a dictionary whose keys are the unique values from
              > the list. I got the idea from a discussion here on comp.lang.pytho n; I
              > called my version of it tally().
              >
              > d = tally(bool(x) for x in seq)
              > print d[True] # prints how many true values in seq
              > print d[False] # prints how many false values in seq
              >
              >
              > tally() is in my iterwrap.py module, which you can get here:
              >
              > http://home.blarg.net/~steveha/iterwrap.tar.gz
              >[/color]
              [color=blue][color=green][color=darkred]
              >>> from itertools import groupby
              >>> tally = lambda it : dict((x,sum(1 for _ in y)) for x,y in groupby(sorted( it)))
              >>> tally('abbcabbc ca')[/color][/color][/color]
              {'a': 3, 'c': 3, 'b': 4}

              Comment

              Working...