Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #60624

Re: Wrapping around a list

From Peter Pearson <ppearson@nowhere.invalid>
Newsgroups comp.lang.python
Subject Re: Wrapping around a list
Date 2013-11-27 17:42 +0000
Message-ID <bfmp86F1fl3U1@mid.individual.net> (permalink)
References <35a56651-33b3-454e-a936-439196989d3b@googlegroups.com> <mailman.3299.1385559225.18130.python-list@python.org>

Show all headers | View raw


On Wed, 27 Nov 2013 08:33:43 -0500, Neil Cerutti <mr.cerutti@gmail.com> wrote:
> 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.

The original poster's combinations comprise letters that are *contiguous*,
in a circular sense.  Yours include non-contiguous sets, like LQ.

-- 
To email me, substitute nowhere->spamcop, invalid->net.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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