我有一些非常低效的代码,我想更通用/更高效.我试图从一组列表中创建字符串.
这是我目前拥有的:
#contains categories
numind = [('Length',),('Fungus',)]
#contains values that pertain to the categories
records = [('Length','Long'),('Length','Med'),'Yes'),'No')]
#contains every combination of values between the 2 categories.
#for example,(Long,Yes) = Length=Long & Fungus = Yes.
combinations = [('Long',('Long','No'),('Med','No')]
现在我想创建在我的组合列表中包含每个组合的字符串.这是效率低下的部分.我喜欢它,所以我不必硬连接“numind”列表的长度.有任何想法吗?
values = combinations
valuestring = []
if len(numind) == 0:
pass
elif len(numind) == 1:
for a in xrange(len(values)):
valuestring.append(numind[0][0]+values[a][0])
elif len(numind) == 2:
for a in xrange(len(values)):
valuestring.append(numind[0][0]+values[a][0]+'_'+numind[1][0]+values[a][1])
#and so forth until numind is 10+
产量
['LengthLong_FungusYes','LengthLong_FungusNo','LengthMed_FungusYes','LengthMed_FungusNo']
最佳答案
我将itertools.product与collections.OrderedDict一起使用(后者并非绝对必要,但意味着您无需考虑就能获得正确的订单):
原文链接:https://www.f2er.com/python/439607.html>>> from collections import OrderedDict
>>> from itertools import product
>>>
>>> d = OrderedDict()
>>> for k,v in records:
... d.setdefault(k,[]).append(v)
...
>>> d
OrderedDict([('Length',['Long','Med']),['Yes','No'])])
>>> ['_'.join(k+v for k,v in zip(d,v)) for v in product(*d.values())]
['LengthLong_FungusYes','LengthMed_FungusNo']
itertools.product自然地产生“每个组合”部分(实际上称为笛卡尔积,而不是组合):
>>> product(["Long","Med"],["Yes","No"])
这里的优点是,无论有多少类别或与任何类别相关联的值有多少都是无关紧要的:只要它们在记录中指定,它就应该有效.