我需要做一些必须很容易的事情,但由于某些原因,我无法做到。
因此,基本上,我需要生成一个新的列表,它可以将三个列表中的项目连接起来。
请注意,这三个列表目前都在字典a中。
为了清晰起见,我想做的是:
def test(phrase):
a = {}
tmp = phrase.split(' ')
for m in xrange(0,len(tmp)):
a[m] = anagrammes2(tmp[m])
# The anagram function will spit out the anagrams of the words
# a[1] = ['mange']
# a[2] = ['ton, 'ont']
# a[3] = ['orange','onagre','organe','rongea']
test('Mange ton orange')
#result should be:
['mange ont onagre', 'mange ont orange', 'mange ont orangé', 'mange ont organe',
'mange ont rongea', 'mange ton onagre', 'mange ton orange', 'mange ton orangé',
'mange ton organe', 'mange ton rongea', 'mangé ont onagre', 'mangé ont orange',
'mangé ont orangé', 'mangé ont organe', 'mangé ont rongea',
'mangé ton onagre', 'mangé ton orange', 'mangé ton orangé',
'mangé ton organe', 'mangé ton rongea']发布于 2016-02-17 12:49:37
您可以使用itertools.product()
>>> a = [['mange'], ['ton', 'ont'], ['orange','onagre','organe','rongea']]
>>> from itertools import product
>>> [' '.join(x) for x in product(*a)]
['mange ton orange',
'mange ton onagre',
'mange ton organe',
'mange ton rongea',
'mange ont orange',
'mange ont onagre',
'mange ont organe',
'mange ont rongea']与您的代码集成:
def test(phrase):
anas = [anagrammes2(word) for word in phrase.split(' ')]
return [' '.join(x) for x in product(*anas)]
test('Mange ton orange')发布于 2016-02-17 12:22:41
假设它们是字符串:
result = tuple(listA[x % len(listA)] + listB[x % len(listB)] + listC[x % len(listC)] for x in range(max(len(listA), len(listB), len(listC))))https://stackoverflow.com/questions/35456478
复制相似问题