Bob Gailer wrote:
I just noticed missing ) in the last 2 statements. Fixed below.
Johan Geldenhuys wrote:
Hi all,
I found this function that converts a integer into a 8 bit
binary string.
Would somebody care to explain what is happening in this
process?
def intToBin(self, x, count=8):
""" Parameters: `x`: integer
Returns a 8 bit binary string of x """
return "".join(map(lambda y:str((x>>y)&1), range(count-1, -1, -1)))
I could tell you what it does, but to support your learning I encourage
you to take it apart piece by piece and look up anything you don't
know. Tell us what (if any) pieces of this you do understand. The
"pieces" are (innermost out):
>>
&
str()
lambda
range()
map()
join()
Also recognize that map(lambda...) is now done more clearly using list
comprehension:
"".join([str((x>>y)&1) for y in range(count-1, -1, -1)])
or if you are using the latest python use a generator _expression_:
"".join(str((x>>y)&1) for y in range(count-1, -1, -1))
--
Bob Gailer
510-978-4454
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
--
Bob Gailer
510-978-4454
|
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor