Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'hettinger': 0.07; 'seemed': 0.07; 'python': 0.09; '(it': 0.09; 'lines:': 0.09; 'string)': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr :python-list': 0.10; 'def': 0.10; 'itertools': 0.16; 'lambda': 0.16; 'meanwhile': 0.16; 'nick': 0.16; 'respondents': 0.16; 'thoughts?': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'import': 0.21; '(all': 0.22; 'subject:skip:i 10': 0.22; 'wednesday,': 0.22; 'cc:2**0': 0.23; 'raymond': 0.23; 'split': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:" 20': 0.26; 'module.': 0.27; "doesn't": 0.28; 'lines': 0.28; 'along.': 0.29; 'behaving': 0.29; 'description,': 0.29; 'whitespace': 0.29; 'url:mailman': 0.29; 'case,': 0.29; 'words': 0.29; "i'm": 0.29; 'e.g.': 0.30; 'code': 0.31; 'december': 0.32; 'url:python': 0.32; 'file': 0.32; 'url:listinfo': 0.32; '2007,': 0.33; 'null': 0.33; 'shorter': 0.33; 'anyone': 0.33; 'guys': 0.33; 'themselves': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'text': 0.34; 'list': 0.35; 'fresh': 0.35; 'mine': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'url:org': 0.36; 'skip:p 20': 0.36; 'turn': 0.36; 'best,': 0.37; 'does': 0.37; 'two': 0.37; 'drop': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'things': 0.38; 'sure': 0.38; 'description': 0.39; 'space': 0.39; 'skip:" 10': 0.40; 'end': 0.40; 'url:mail': 0.40; 'your': 0.60; 'red': 0.60; 'mentioned': 0.63; 'love': 0.63; 'more': 0.63; 'today,': 0.64; 'said:': 0.65; 'shortly': 0.65; 'hours': 0.66; 'url:bit': 0.66; 'url:ly': 0.66; 'account': 0.67; 'felt': 0.75; 'complex,': 0.84; 'words)': 0.84; 'heat': 0.91; 'subject:Good': 0.91 Newsgroups: comp.lang.python Date: Tue, 4 Dec 2012 07:24:07 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=124.170.17.135; posting-account=ZAg6xAoAAAAmY8bBi3VzYjWntm8Ct1P8 References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 124.170.17.135 MIME-Version: 1.0 Subject: Re: Good use for itertools.dropwhile and itertools.takewhile From: Nick Mellor To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org 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: , Message-ID: Lines: 185 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354635214 news.xs4all.nl 6882 [2001:888:2000:d::a6]:37492 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34237 I love the way you guys can write a line of code that does the same as 20 of mine :) I can turn up the heat on your regex by feeding it a null description or multiple white space (both in the original file.) I'm sure you'd adjust, but at the cost of a more complex regex. Meanwhile takewith and dropwith are behaving themselves impeccably but my while loop has fallen over. Best, Nick On Wednesday, 5 December 2012 01:31:48 UTC+11, Vlastimil Brom wrote: > 2012/12/4 Nick Mellor : > > > Hi, > > > > > > I came across itertools.dropwhile only today, then shortly afterwards found Raymond Hettinger wondering, in 2007, whether to drop [sic] dropwhile and takewhile from the itertools module. > > > > > > Fate of itertools.dropwhile() and itertools.takewhile() - Python > > > bytes.com > > > http://bit.ly/Vi2PqP > > > > > > Almost nobody else of the 18 respondents seemed to be using them. > > > > > > And then 2 hours later, a use case came along. I think. Anyone have any better solutions? > > > > > > I have a file full of things like this: > > > > > > "CAPSICUM RED fresh from Queensland" > > > > > > Product names (all caps, at start of string) and descriptions (mixed case, to end of string) all muddled up in the same field. And I need to split them into two fields. Note that if the text had said: > > > > > > "CAPSICUM RED fresh from QLD" > > > > > > I would want QLD in the description, not shunted forwards and put in the product name. So (uncontrived) list comprehensions and regex's are out. > > > > > > I want to split the above into: > > > > > > ("CAPSICUM RED", "fresh from QLD") > > > > > > Enter dropwhile and takewhile. 6 lines later: > > > > > > from itertools import takewhile, dropwhile > > > def split_product_itertools(s): > > > words = s.split() > > > allcaps = lambda word: word == word.upper() > > > product, description = takewhile(allcaps, words), dropwhile(allcaps, words) > > > return " ".join(product), " ".join(description) > > > > > > > > > When I tried to refactor this code to use while or for loops, I couldn't find any way that felt shorter or more pythonic: > > > > > > (9 lines: using for) > > > > > > def split_product_1(s): > > > words = s.split() > > > product = [] > > > for word in words: > > > if word == word.upper(): > > > product.append(word) > > > else: > > > break > > > return " ".join(product), " ".join(words[len(product):]) > > > > > > > > > (12 lines: using while) > > > > > > def split_product_2(s): > > > words = s.split() > > > i = 0 > > > product = [] > > > while 1: > > > word = words[i] > > > if word == word.upper(): > > > product.append(word) > > > i += 1 > > > else: > > > break > > > return " ".join(product), " ".join(words[i:]) > > > > > > > > > Any thoughts? > > > > > > Nick > > > -- > > > http://mail.python.org/mailman/listinfo/python-list > > > > Hi, > > the regex approach doesn't actually seem to be very complex, given the > > mentioned specification, e.g. > > > > >>> import re > > >>> re.findall(r"(?m)^([A-Z\s]+) (.+)$", "CAPSICUM RED fresh from QLD\nCAPSICUM RED fresh from Queensland") > > [('CAPSICUM RED', 'fresh from QLD'), ('CAPSICUM RED', 'fresh from Queensland')] > > >>> > > > > (It might be necessary to account for some punctuation, whitespace etc. too.) > > > > hth, > > vbr