NEWBIE: map | zip | list comp

Terry Reedy tjreedy at udel.edu
Fri Jan 2 00:46:18 EST 2004


<engsolnom at ipns.com> wrote in message
news:odv9vvgb4aobq4c617k0ju4plat2d81j0o at 4ax.com...
> Hello again...hope everyone had a great New Year...:)
>
> In the quest to learn more about Python, I decided to create an arbitrary
sequence,
> then march each member through a 'field' of each of the members in turn,
and just to complicate it a
> bit, make the length of the field user defined.
> Hard to explain...an example might serve:
>
> 10000
> 01000
> 00100
>   .
>   .
> 00009
> 01111
> 10111
> 11011
>   .
>   .
> 21111
> 12111
> 11211
> and so forth.
>
> The only coding I could come up with is:
>
> base_seq = 'ABCD'
> length = 6
>
> for char_1 in base_seq:                          # Loop thru the set
char_1 at a time
>     for char_2 in base_seq:                     # Loop thru the seq again
for each char_1
>         if char_2 == char_1: continue         # Don't march 'A' thru a
field of 'A's
>         for ix in range(length):                     # Now march char_2
thru a field of char_1's
>             print (char_1 * length)[:ix] + char_2 + (char_1 *
length)[ix:-1]

Move the constant char_1*length outside of the two inner loops, or else
multiple by the actual needed length instead of making too much and
slicing.

>
> which, as you can see, has three nested FOR loops. I played with map, zip
and list comps a bit, but
> couldn't figure out how to compress the above code.

You mean something like (untested):

[char_1*ix + char_2 + char_1*(length - ix -1) for char_1 in base_seq
  for char_2 in base_seq if char_1 != char_2 for ix in range(length)]

> Am I barking up the wrong Python tree?

I think so.  Consider how long it took you to write clear conprehensible
code using Python's significant indentation *feature* and how long it took
you to not successfully get rid of it and how long it took you to convince
yourself that my no-indent perversion produces the same items (though in a
list), if indeed it does.  In my view, expressions should be only as long
as can be grokked in one mental gulp, and I think above overdoes it a bit.
Of course, the humancode quality can be restrored by writing it more
clearly as

[char_1*ix + char_2 + char_1*(length - ix -1)
    for char_1 in base_seq
        for char_2 in base_seq
            if char_1 != char_2
               for ix in range(length)]

in which case one has just moved the last line to the top and avoided
result=[] and result.append().

Terry J. Reedy





More information about the Python-list mailing list