Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'anyway.': 0.05; 'true,': 0.05; 'none:': 0.07; 'string': 0.09; 'logic': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'idea:': 0.16; 'inputs': 0.16; 'letters.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'scalable,': 0.16; 'simple.': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'wed,': 0.18; 'typing': 0.19; 'input': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'rapidly': 0.24; 'received:comcast.net': 0.24; 'fairly': 0.24; 'skip:v 30': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; '(like': 0.30; 'operations,': 0.30; 'subject:list': 0.30; 'code': 0.31; 'reply.': 0.31; 'with,': 0.31; 'becomes': 0.33; 'actual': 0.34; 'there,': 0.34; 'core': 0.34; 'maybe': 0.34; "i'd": 0.34; 'problem.': 0.35; 'but': 0.35; 'really': 0.36; 'doubt': 0.36; 'yield': 0.36; 'thanks': 0.36; 'performance': 0.37; 'nov': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'commands': 0.60; 'simple': 0.61; "you'll": 0.62; 'talking': 0.65; 'million': 0.74; '100': 0.79; 'cpu,': 0.84; 'syed': 0.84; 'hundred': 0.95; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Batchelder Subject: Re: Wrapping around a list Date: Wed, 27 Nov 2013 06:26:26 -0500 References: <35a56651-33b3-454e-a936-439196989d3b@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: c-50-133-228-126.hsd1.ma.comcast.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.1.1 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385551604 news.xs4all.nl 15933 [2001:888:2000:d::a6]:59646 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60595 On 11/27/13 6:14 AM, Chris Angelico wrote: > On Wed, Nov 27, 2013 at 10:07 PM, Amjad Syed wrote: >> Thanks Chris for the reply. But i would like sliding function to be scalable, as input string can be of 100 letters. > > A hundred isn't much to work with, and your code will be fairly > simple. Give it a try with small strings, see how it goes; then try it > on your full-size - I doubt there'll be a problem. > > Now, if you were talking about a hundred million letters, then maybe > there'd be a problem. But even there, I'd start with the simple and > clean approach, and only optimize for performance when it becomes > obviously necessary (like when my MUD client was able to saturate one > core of my CPU, just by me typing a lot of commands very rapidly - > that needed fixing!). > > ChrisA > Using ChrisA's idea: def sliding_window(iterable, n, fill=None): values = list(iterable) num_values = len(values) if fill is not None: values.extend([fill]*(n-1)) need_more = (2*num_values-1) - len(values) values.extend(values[:need_more]) for start in range(num_values): yield values[start:start+n] l = list("LEQNABC") for n in range(2, len(l)+1): print [''.join(x) for x in sliding_window(l, n)] for n in range(2, len(l)+1): print [''.join(x) for x in sliding_window(l, n, fill="_")] Produces: ['LE', 'EQ', 'QN', 'NA', 'AB', 'BC', 'CL'] ['LEQ', 'EQN', 'QNA', 'NAB', 'ABC', 'BCL', 'CLE'] ['LEQN', 'EQNA', 'QNAB', 'NABC', 'ABCL', 'BCLE', 'CLEQ'] ['LEQNA', 'EQNAB', 'QNABC', 'NABCL', 'ABCLE', 'BCLEQ', 'CLEQN'] ['LEQNAB', 'EQNABC', 'QNABCL', 'NABCLE', 'ABCLEQ', 'BCLEQN', 'CLEQNA'] ['LEQNABC', 'EQNABCL', 'QNABCLE', 'NABCLEQ', 'ABCLEQN', 'BCLEQNA', 'CLEQNAB'] ['LE', 'EQ', 'QN', 'NA', 'AB', 'BC', 'C_'] ['LEQ', 'EQN', 'QNA', 'NAB', 'ABC', 'BC_', 'C__'] ['LEQN', 'EQNA', 'QNAB', 'NABC', 'ABC_', 'BC__', 'C___'] ['LEQNA', 'EQNAB', 'QNABC', 'NABC_', 'ABC__', 'BC___', 'C____'] ['LEQNAB', 'EQNABC', 'QNABC_', 'NABC__', 'ABC___', 'BC____', 'C_____'] ['LEQNABC', 'EQNABC_', 'QNABC__', 'NABC___', 'ABC____', 'BC_____', 'C______'] 100 elements is really nothing, don't try to over-optimize it. And if your inputs are really strings, not general iterables, you can use the same logic but with string operations, and you'll likely have better performance anyway. Less general, true, but better for your actual problem. --Ned.