Wrapping around a list

Neil Cerutti mr.cerutti at gmail.com
Wed Nov 27 08:33:43 EST 2013


On Wed, Nov 27, 2013 at 5:46 AM,  <amjadcsu at gmail.com> wrote:
> I am working on a problem (Bioinformatics domain) where all
> possible combinations of input string needs to be printed as
> sublist
>
> For example:
> Input string : "LEQN"
> Output= "[L","E","Q","N"]
> ["LE","EQ","QN","NL"]["LEQ","EQN","QNE","NLE"]["LEQN"]

How about itertools.combinations?

import itertools

s = "LEQN"
for i in range(len(s)):
    for comb in itertools.combinations(s, i+1):
        print(comb)
Output:
('L',)
('E',)
('Q',)
('N',)
('L', 'E')
('L', 'Q')
('L', 'N')
('E', 'Q')
('E', 'N')
('Q', 'N')
('L', 'E', 'Q')
('L', 'E', 'N')
('L', 'Q', 'N')
('E', 'Q', 'N')
('L', 'E', 'Q', 'N')

For some reason I've got more 2-character combinations than you,
though.

-- 
Neil Cerutti



More information about the Python-list mailing list