def tokenize(data):
cleanup = lambda entry: entry.replace(':', '').strip()
for entry in data.strip(';').split(';'):
entry = map(cleanup, entry.rsplit(':',1))
if len(entry) == 1:
entry.append(True)
yield entry
input = 'name1: value1; name2: value2; name3; prefix: name4: value4;'
print dict(tokenize(input))
{'prefix name4': 'value4', 'name2': 'value2', 'name3': True, 'name1': 'value1'}
>>> dict(re.findall('\s*([\w\s:]+?)\s*(?::\s*([\w\s]*)\s*)?(?=[;$])', input))
{'prefix: name4': 'value4', 'name2': 'value2', 'name3': '', 'name1': 'value1'}
def you_func(d):
result = set()
for k, v in d.items():
r, g, b = k.index('R'), k.index('G'), k.index('B')
for color in v:
result.add((color[r], color[g], color[b]))
return result
def f(d):
return {(c[k.index('R')], c[k.index('G')], c[k.index('B')]) for k, v in d.items() for c in v}
import itertools
lst = [
['aa1','aa2'],
['bb1','bb2','bb3'],
['cc1','cc2','cc3','cc4',],
]
for item in itertools.product(*lst):
# do stuff
print item
>>>
('aa1', 'bb1', 'cc1')
('aa1', 'bb1', 'cc2')
('aa1', 'bb1', 'cc3')
('aa1', 'bb1', 'cc4')
('aa1', 'bb2', 'cc1')
('aa1', 'bb2', 'cc2')
('aa1', 'bb2', 'cc3')
('aa1', 'bb2', 'cc4')
('aa1', 'bb3', 'cc1')
('aa1', 'bb3', 'cc2')
('aa1', 'bb3', 'cc3')
('aa1', 'bb3', 'cc4')
('aa2', 'bb1', 'cc1')
('aa2', 'bb1', 'cc2')
('aa2', 'bb1', 'cc3')
('aa2', 'bb1', 'cc4')
('aa2', 'bb2', 'cc1')
('aa2', 'bb2', 'cc2')
('aa2', 'bb2', 'cc3')
('aa2', 'bb2', 'cc4')
('aa2', 'bb3', 'cc1')
('aa2', 'bb3', 'cc2')
('aa2', 'bb3', 'cc3')
('aa2', 'bb3', 'cc4')
print "".join(chart)
, если в chart уже строки. Иначе print "".join(map(str, chart))