作者e12518166339 (耐纶)
看板Python
标题[问题] 动态追加参数
时间Mon Sep 25 21:50:42 2017
标题下的有点烂,请见谅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)
请问我可以从那个方向来着手呢?
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 180.177.7.3
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1506347445.A.B75.html
1F:→ withoutshine: 想办法生出 args=(gender, birthplayce, education) 09/25 22:06
2F:→ withoutshine: 可以呼叫 itertools.product(*args) 09/25 22:06
3F:推 numpy: 推楼上 09/29 01:38
4F:→ s860134: unpacking 是 python 很方便的用法 09/29 08:46