标题下的有点烂,请见谅QQ
我有一个档案格式长这样
[gender]
Male
Female
[birthplace]
Taipei
Taoyuan
Taichung
Kaohsiung
[other]
music
movie
read
basketball
我想将他做排列组合,我是这样写的
parser = SafeConfigParser(allow_no_value=True)
parser.read('twiwan')
taiwan = []
g = globals()
total_section = parser.sections()
for sSection in total_section:
g['{0}'.format(sSection)] = []
for item in parser.items(sSection):
g[sSection].append(item[0])
for combination in itertools.product(gender, birthplace):
for i in xrange(1, len(other) + 1):
t = list(combinations(other, i))
for element in t:
twiwan.append(list(combination) + list(element))
这里有两个地方写的不够好,第一个是我动态产生list的方式
g['{0}'.format(sSection)] = []
即便不影响正确性,但是他会重复执行
第二个地方是
for combination in itertools.product(gender, birthplace)
因为目前只有gender跟birthplace
如果将来要新增一个新的section
[education]
phD
master
那我势必要回头改这个for循环改成
for combination in itertools.product(gender, birthplace,education)
请问我可以从那个方向来着手呢?