字符串的组合

时间:2017-03-26 21:06:57

标签: python

我有一个变量如下:

symbols = ['KEL','BYCO']

我想要一个能将这些符号组合作为输出的函数,如下所示

['KEL'],['BYCO'] and ['KEL','BYCO']

有人可以推荐任何库/函数,通过它我可以实现这样的组合,给定一个包含n个字符串的变量。

1 个答案:

答案 0 :(得分:0)

我认为这就是你想要的:

Python 2.7.6 (default, Oct 26 2016, 20:30:19)                                                                                                                                                                                                                                  
[GCC 4.8.4] on linux2                                                                                                                                                                                                                                                          
Type "help", "copyright", "credits" or "license" for more information.                                                                                                                                                                                                         
>>>                                                                                                                                                                                                                                                                            
>>> from itertools import combinations, chain                                                                                                                                                                                                                                  
>>>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
>>> def powerset(iterable):                                                                                                                                                                                                                                                    
...     "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"                                                                                                                                                                                                    
...     s = list(iterable)                                                                                                                                                                                                                                                     
...     return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))                                                                                                                                                                                                
...                                                                                                                                                                                                                                                                            
>>> symbols = ['KEL','BYCO']
>>>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
>>> list(powerset(symbols))                                                                                                                                                                                                                                                  
[(), ('KEL',), ('BYCO',), ('KEL', 'BYCO')]

此处的powerset功能直接取自文档:https://docs.python.org/2/library/itertools.html