2014-03-27 33 views
3

我有以下要求。将列表中的元素与所有可能的分隔符结合使用

我有说有3个要素[X,Y,2]

我想这样做是产生一个分隔字符串列表(比如“ - ”)(或不能)各元素之间。数组中元素的顺序应该保留。

所以输出将是:

'XY2' 
'X-Y-2' 
'X-Y2' 
'XY-2' 

是有一种优雅的方式来这蟒蛇?

+0

你的意思是没有分隔符?另外,这些并不是真正的组合,只是将分隔符放到所有可能的位置。 – KobeJohn

+0

是的,我很抱歉。元素的顺序应该保留。我会做出改变 – suzee

回答

4
>>> import itertools 
>>> for c in itertools.product(' -', repeat=2): print ('X%sY%s2' % c).replace(' ', '') 
XY2 
XY-2 
X-Y2 
X-Y-2 

或者,从Python列表来的元素:

import itertools 
a = ['X', 'Y', 2] 
for c in itertools.product(' -', repeat=2): 
    print ('%s%s%s%s%s' % (a[0],c[0],a[1],c[1],a[2])).replace(' ', '') 

或者,在一个稍微不同的风格:

import itertools 
a = ['X', 'Y', '2'] 
for c in itertools.product(' -', repeat=2): 
    print ('%s'.join(a) % c).replace(' ', '') 

捕获输出到一个列表:

import itertools 
a = ['X', 'Y', '2'] 
output = [] 
for c in itertools.product(' -', repeat=len(a)-1): 
    output.append(('%s'.join(a) % c).replace(' ', '')) 
print 'output=', output 
+0

不错,但列表在哪里? :) –

+0

@RaulGuiu好吧,我已经添加了两个表单作为源。让我知道,如果他们是你在找什么。 – John1024

+0

这太好了。谢谢@ John1024。将结果添加到另一个列表。可以修改为:a = [X,Y,2] b = [] for itertools.product(' - ',repeat = len(a)-1):b.append(str(“%s” .aoin(a)%c).replace('','')) – suzee

0

像这样?

from itertools import permutations 

i = ["X","Y","2"] 
for result in permutations(i, 3): 
    print "-".join(result) 

结果:

X-Y-2 
X-2-Y 
Y-X-2 
Y-2-X 
2-X-Y 
2-Y-X 
+0

嗨Monkut。谢谢。但是列表中元素的顺序应该保留。我正在寻找的输出在问题中指定。 – suzee

2

一点更广义的,但适用于任何数量的分隔符,希望很容易在每一步明白:

import itertools 
a = ['X', 'Y', '2'] 
all_separators = ['', '-', '+'] 

results = [] 
# this product puts all separators in all positions for len-1 (spaces between each element) 
for this_separators in itertools.product(all_separators, repeat=len(a)-1): 
    this_result = [] 
    for pair in itertools.izip_longest(a, this_separators, fillvalue=''): 
     for element in pair: 
      this_result.append(element) 
    # if you want it, here it is as a comprehension 
    # this_result = [element for pair 
    #    in itertools.izip_longest(a, this_separators, fillvalue='') 
    #    for element in pair] 
    this_result_string = ''.join(this_result) # check out join docs if it's new to you 
    results.append(this_result_string)  

print results 
>>> ['XY2', 'XY-2', 'XY+2', 'X-Y2', 'X-Y-2', 'X-Y+2', 'X+Y2', 'X+Y-2', 'X+Y+2'] 

这些是结果你的情况下,只有“”和“ - ”作为分隔符:

>>> ['XY2', 'XY-2', 'X-Y2', 'X-Y-2'] 

如果你想要的一切,在一个理解:

results = [''.join(element for pair 
        in itertools.izip_longest(a, this_separators, fillvalue='') 
        for element in pair) 
      for this_separators in itertools.product(all_separators, repeat=len(a)-1)] 
1

我不知道在itertool中是否有函数来实现这个功能。但我一直认为这样做很有趣,也是一种很好的练习。所以有一个递归生成器的解决方案:

def generate(liste): 
    if len(liste) == 1: 
     yield [liste] 
    else: 
     for i in generate(liste[1:]): 
      yield [[liste[0]]]+i 
      yield [ [liste[0]]+i[0] ] + i[1:] 

if __name__ == "__main__": 
    for i in generate (["X","Y","2"]): 
     print "test : " + str(i) 
     if len(i) == 1: 
      print "".join(i[0]) 
     else: 
      print reduce(
       lambda left, right : left + "".join(right), 
       i, 
      "") 
相关问题