<html>
<head>
</head>
<body>
<br>
<br>
Thorsten Kampe wrote:<br>
<blockquote type="cite" cite="mid:aljdhe$1q63hj$1@ID-77524.news.dfncis.de">
  <pre wrap="">"Python, anyone?"... Yep, this is my first one. Comments on<br>style/readability and technique/flow appreciated!</pre>
  </blockquote>
Looks good overall, certainly you're off to a good start.<br>
  <br>
I added a few remarks/answers below...<br>
  <br>
--jb<br>
  <blockquote type="cite" cite="mid:aljdhe$1q63hj$1@ID-77524.news.dfncis.de">
    <pre wrap=""><br><br>#v+<br>def quotient_set(seq, func, partition='nonbool'):<br>    """ partition <seq> into equivalence classes<br><br>    With the 'bool' parameter 'quotient_set' always returns two lists: the<br>    first  containing the 'true' items and the second containing the false<br>    ones. """<br>    if partition == 'bool':<br>    # Yes, I know about 'filter', but this returns just the 'true<br>    # elements'...<br>    <br>        # put this in one line?<br>        true_class  = []<br>        false_class = []</pre>
    </blockquote>
    <br>
         #### if you are thinking "true_class = []; false_class - []" then
I'd guess<br>
         #### most here would recommend against bunching up as pointless<br>
         #### and just a tad less readable than two separate statements.<br>
    <pre wrap=""></pre>
        #### if you are thinking "true_class = false_class = []"<br>
        #### then it would be a BIG NO-NO.<br>
        #### the latter form initializes both variables to the SAME (empty)
list<br>
        #### and I'm pretty sure you want 2 separate lists.  <br>
    <br>
        #### 'Mutable' constant objects like [] and {} work differently from<br>
        #### 'Immutable' ones like 1, 0.0 and "abc".  Not sure the precise
Pythonetics <br>
        #### terminology, but one way to think of it is that mutable types
are accessed <br>
        #### 'by reference' while immutableones are accessed as if they were
'by value'.<br>
        #### 2 + 3 results in a new number.  list.append(...) changes the
list in place and<br>
        #### affects all 'references' to the list.  <br>
    <br>
    <blockquote type="cite" cite="mid:aljdhe$1q63hj$1@ID-77524.news.dfncis.de">
      <pre wrap=""><br>        for item in seq:<br>            if func(item):<br>                true_class.append(item)<br>            else:<br>                false_class.append(item)<br>        return [true_class, false_class]<br>        <br>    else:<br>        # put this in one line?<br>        quotient_set =         []<br>        representative_class = []</pre>
      </blockquote>
      <blockquote type="cite" cite="mid:aljdhe$1q63hj$1@ID-77524.news.dfncis.de">
        <pre wrap="">   for item in seq:<br>            representative = func(item)<br><br>            # "Try: seq.index(item)" is IMO rather ugly in a loop. But<br>            # it's even better than "if item in seq: seq.index(item)",<br>            # which goes twice through seq<br>            try:<br>                quotient_set_index = representative_class.index(representative)<br>            except ValueError:<br>                quotient_set.append([item])</pre>
        </blockquote>
        <br>
#### teeny tiny nit:  I prefer "quotient_set.extend( item )" to the above.<br>
        <blockquote type="cite" cite="mid:aljdhe$1q63hj$1@ID-77524.news.dfncis.de">
          <pre wrap=""><br>                representative_class.append(representative)            <br>            else:<br>                # This somehow "inelegant", because all I want to do is:<br>                # "seq[index] = seq[index].append(item)" Suggestions?</pre>
          </blockquote>
          <br>
####  Define your set or sequence types to be a class or classes.  <br>
####  You can then define [] and .append() operations <br>
####  to be whatever you want (e.g., silently exclude duplicates, etc.)<br>
          <br>
####  Even the most trivial data abstractions usually benefit by being made
an<br>
 ####  explicit class/object.<br>
          <pre wrap=""></pre>
####  if you are 'indexing' you may want to use a dictionary base type instead
of a list.<br>
          <br>
####  if you only want to test membership, instead of the 'try' you can test:<br>
          <br>
                  if representative in representative_class:  ....  <br>
          <br>
          <br>
          <blockquote type="cite" cite="mid:aljdhe$1q63hj$1@ID-77524.news.dfncis.de">
            <pre wrap=""><br>                equivalence_class = quotient_set[quotient_set_index]<br>                equivalence_class.append(item)<br>                quotient_set[quotient_set_index] = equivalence_class<br>                <br>        return quotient_set<br>#v-<br><br>To get a feeling for what 'quotient_set' does, try:<br></pre>
            <blockquote type="cite">
              <blockquote type="cite">
                <blockquote type="cite">
                  <pre wrap="">f = lambda x: x % 3<br>quotient_set([1, 2, 3, 4, 5, 6, 7, 8, 9], f)<br></pre>
                  </blockquote>
                  </blockquote>
                  </blockquote>
                  <pre wrap=""><!---->[[1, 4, 7], [2, 5, 8], [3, 6, 9]]<br><br></pre>
                  <blockquote type="cite">
                    <blockquote type="cite">
                      <blockquote type="cite">
                        <pre wrap="">quotient_set([1, 2, 3, 4, 5, 6, 7, 8, 9], f, 'bool')<br></pre>
                        </blockquote>
                        </blockquote>
                        </blockquote>
                        <pre wrap=""><!---->[[1, 2, 4, 5, 7, 8], [3, 6, 9]]<br><br>#v+<br>def set(seq):<br>    """ make <seq> a true set by removing duplicates """<br>    # Shortest way to do a 'null function'? 'None' doesn't work...<br>    f = lambda x: x  # identity function<br>    <br>    return [item[0] for item in quotient_set(seq, f)]    <br>#v-<br></pre>
                        </blockquote>
                        <br>
                        <pre class="moz-signature" cols="$mailwrapcol">-- 
James J. Besemer                503-280-0838 voice
2727 NE Skidmore St.            503-280-0375 fax
Portland, Oregon 97211-6557     <a class="moz-txt-link-freetext" href="mailto:jb@cascade-sys.com">mailto:jb@cascade-sys.com</a>
                                <a class="moz-txt-link-freetext" href="http://cascade-sys.com">http://cascade-sys.com</a>       
</pre>
                        <br>
                        </body>
                        </html>