[Pythonmac-SIG] algorithm wanted

has hengist.podd at virgin.net
Wed Feb 2 01:47:13 CET 2005


Henning Hraban Ramm wrote:

>I'm looking for a better algorithm...

This is probably as terse as it gets:


def bool2cm0k(lst):
     """Converts a list of booleans to a CMYK color string,
         e.g. [True, False, False, True] to 'C00K'
     """
     return ''.join([b and c or '0' for b, c in zip(lst, 'CMYK')])

def cm0k2bool(s):
     """Converts a CMYK color string to a list of booleans,
         e.g. 'CM0K' to [True, True, False, True]
     """
     return [c1 == c2 for c1, c2 in zip(s, 'CMYK')]

print bool2cm0k([True, False, False, True]) # --> C00K
print cm0k2bool('CM0K') # --> [True, True, False, True]


HTH

has
-- 
http://freespace.virgin.net/hamish.sanderson/


More information about the Pythonmac-SIG mailing list