1

I have a variable as follows:

symbols = ['KEL','BYCO']

I want a function that would give combination of these symbols as output as shown below

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

Could someone recommend any library/function through which I can achieve such combinations given a variable which contains n number of strings in it.

Furqan Hashim
  • 1,304
  • 2
  • 15
  • 35
  • What you are looking for looks like a *powerset*. The empty list is also a combination... You can use `itertools.combinations`... – Willem Van Onsem Mar 26 '17 at 21:08
  • I have looked onto that function. In the itertools.combination(iterable,r) what would r be? In one instance I need it to be 1 to achieve first two elements of my output and then I need r to be 2 to get the last output. Hence it didn't worked for me. – Furqan Hashim Mar 26 '17 at 21:12
  • well now you only need to increment the length and generate combinations of that length. Do you want to include the power-set? – Willem Van Onsem Mar 26 '17 at 21:13
  • Search for powerset in `https://docs.python.org/2/library/itertools.html`. – Alex Hall Mar 26 '17 at 21:18

1 Answers1

0

I think this is what you want:

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')]

The powerset function here is lifted straight from the documentation: https://docs.python.org/2/library/itertools.html

Newb
  • 2,810
  • 3
  • 21
  • 35