Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; "'')": 0.07; 'prefix': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'def': 0.10; 'applies': 0.15; 'itertools': 0.16; 'omitted.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'trivially': 0.16; 'string': 0.17; 'wrote:': 0.17; 'jan': 0.18; '>>>': 0.18; 'skip:p 30': 0.20; 'import': 0.21; 'subject:skip:i 10': 0.22; 'split': 0.23; 'idea': 0.24; 'header :In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.28; 'index,': 0.29; 'remains': 0.29; 'words': 0.29; "skip:' 10": 0.30; "skip:' 20": 0.32; 'to:addr:python-list': 0.33; 'fresh': 0.35; 'pm,': 0.35; 'list.': 0.35; 'received:org': 0.36; 'rather': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'description': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'red': 0.60; 'received:fios.verizon.net': 0.84; 'subject:Good': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Good use for itertools.dropwhile and itertools.takewhile Date: Tue, 04 Dec 2012 17:21:33 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20121026 Thunderbird/16.0.2 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354659896 news.xs4all.nl 6890 [2001:888:2000:d::a6]:43227 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34261 On 12/4/2012 3:44 PM, Terry Reedy wrote: > If the original string has no excess whitespace, description is what > remains of s after product prefix is omitted. (Py 3 code) > > from itertools import takewhile > def allcaps(word): return word == word.upper() > > def split_product_itertools(s): > product = ' '.join(takewhile(allcaps, s.split())) > return product, s[len(product)+1:] > > print(split_product_itertools("CAPSICUM RED fresh from QLD")) > >>> > ('CAPSICUM RED', 'fresh from QLD') > > Without that assumption, the same idea applies to the split list. > > def split_product_itertools(s): > words = s.split() > product = list(takewhile(allcaps, words)) > return ' '.join(product), ' '.join(words[len(product):]) Because these slice rather than index, either works trivially on an empty description. print(split_product_itertools("CAPSICUM RED")) >>> ('CAPSICUM RED', '') -- Terry Jan Reedy