Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60606
| References | <35a56651-33b3-454e-a936-439196989d3b@googlegroups.com> |
|---|---|
| Date | 2013-11-27 08:33 -0500 |
| Subject | Re: Wrapping around a list |
| From | Neil Cerutti <mr.cerutti@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3299.1385559225.18130.python-list@python.org> (permalink) |
On Wed, Nov 27, 2013 at 5:46 AM, <amjadcsu@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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Wrapping around a list amjadcsu@gmail.com - 2013-11-27 02:46 -0800
Re: Wrapping around a list Chris Angelico <rosuav@gmail.com> - 2013-11-27 21:53 +1100
Re: Wrapping around a list Amjad Syed <amjadcsu@gmail.com> - 2013-11-27 03:07 -0800
Re: Wrapping around a list Chris Angelico <rosuav@gmail.com> - 2013-11-27 22:14 +1100
Re: Wrapping around a list Amjad Syed <amjadcsu@gmail.com> - 2013-11-27 10:14 -0800
Re: Wrapping around a list Ned Batchelder <ned@nedbatchelder.com> - 2013-11-27 06:26 -0500
Re: Wrapping around a list Neil Cerutti <mr.cerutti@gmail.com> - 2013-11-27 08:33 -0500
Re: Wrapping around a list Peter Pearson <ppearson@nowhere.invalid> - 2013-11-27 17:42 +0000
Re: Wrapping around a list rusi <rustompmody@gmail.com> - 2013-11-27 08:12 -0800
csiph-web