2

I was watching Brandon Rhodes talk on Dictionary where I found him using a generator expression

diff = ('^ '[a==b] for a,b in zip(k1, k2))

where k1 and k2 are binary representations of some numbers. For every bit in k1 and k2 which are not identical this generator expression puts a '^ ' to mark the position. I haven't been able to understand this expression. What it means?

thefourtheye
  • 206,604
  • 43
  • 412
  • 459
anukalp
  • 2,480
  • 5
  • 13
  • 23
  • 1
    Especially [this answer](http://stackoverflow.com/a/470376/1903116) in the linked question. – thefourtheye Nov 05 '16 at 13:34
  • 2
    That's a 'hidden' conditional expression; the Python boolean type is a subclass of `int` and `False` has the numeric value `0`, `True` has the numeric value `1`, so based on that either `'^'` or `' '` is picked. – Martijn Pieters Nov 05 '16 at 13:34
  • 1
    Hint: try `'^ '[False]` and then `'^ '[True]` in the interactive shell. – dawg Nov 05 '16 at 13:35
  • 4
    The code would be much improved with an actual conditional expression: `(' ' if a == b else '^' for a, b in zip(k1, k2))`. – Martijn Pieters Nov 05 '16 at 13:35

0 Answers0