Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.05; 'string.': 0.05; 'context': 0.07; 'string': 0.09; 'iterate': 0.09; 'messing': 0.09; 'none)': 0.09; 'propagate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:extra': 0.09; 'def': 0.12; '(date': 0.16; '1):': 0.16; 'dict': 0.16; 'dict(': 0.16; 'frequencies': 0.16; 'igor': 0.16; 'key?': 0.16; 'keys.': 0.16; 'looping': 0.16; 'merely': 0.16; 'namedtuples': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'stats': 0.16; 'subject:variable': 0.16; '{})': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'library': 0.18; 'variable': 0.18; 'bit': 0.19; 'items.': 0.19; 'written': 0.21; 'seems': 0.21; 'appears': 0.22; 'header:User-Agent:1': 0.23; 'helper': 0.24; 'rid': 0.24; 'skip': 0.24; 'string,': 0.24; 'source': 0.25; 'switch': 0.26; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'raise': 0.29; 'tim': 0.29; 'said,': 0.30; "i'm": 0.30; 'code': 0.31; 'chase': 0.31; 'keys': 0.31; 'occurs': 0.31; 'up.': 0.33; 'skip:d 20': 0.34; "i'd": 0.34; 'something': 0.35; 'convert': 0.35; 'objects': 0.35; 'one,': 0.35; 'point.': 0.35; 'but': 0.35; 'version': 0.36; 'really': 0.36; 'data,': 0.36; 'dates': 0.36; 'sequence': 0.36; 'doing': 0.36; 'next': 0.36; 'should': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'subject:" ': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'even': 0.60; 'skip:z 20': 0.60; "you're": 0.61; 'first': 0.61; 'become': 0.64; 'more': 0.64; 'date,': 0.68; 'improvements': 0.68; '*and*': 0.84; 'eliminates': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Eliminate "extra" variable Date: Sun, 08 Dec 2013 15:04:13 +0100 Organization: None References: <20131206183721.3e7f21c2@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b326.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 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: 110 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386511460 news.xs4all.nl 2958 [2001:888:2000:d::a6]:47458 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61291 Tim Chase wrote: > On 2013-12-06 11:37, Igor Korot wrote: >> def MyFunc(self, originalData): >> data = {} >> for i in xrange(0, len(originalData)): >> dateStr, freq, source = originalData[i] >> data[str(dateStr)] = {source: freq} > > this can be more cleanly/pythonically written as > > def my_func(self, original_data): > for date, freq, source in original_data > data[str(date)] = {source: freq} > > or even just > > data = dict( > (str(date), {source: freq}) > for date, freq, source in original_data > ) or even just data = {str(date): {source: freq} for date, freq, source in original_data} But do you really need a dict with a single key? And is it even correct? If a date occurs twice only the last source:freq pair is kept. Without knowing the context the humble data = {} for date, freq, source in original_data: source_to_freq = data.setdefault(date, {}) if source in source_to_freq: raise ValueError( "Multiple frequencies for one source not supported") source_to_freq[source] = freq appears so much more plausible... > You're calling it a "dateStr", which suggests that it's already a > string, so I'm not sure why you're str()'ing it. So I'd either just > call it "date", or skip the str(date) bit if it's already a string. > That said, do you even need to convert it to a string (as > datetime.date objects can be used as keys in dictionaries)? > >> for i in xrange(0, len(dateStrs) - 1): >> currDateStr = str(dateStrs[i]) >> nextDateStrs = str(dateStrs[i + 1]) >> >> It seems very strange that I need the dateStrs list just for the >> purpose of looping thru the dictionary keys. >> Can I get rid of the "dateStrs" variable? > > Your code isn't actually using the data-dict at this point. If you > were doing something with it, it might help to know what you want to > do. > > Well, you can iterate over the original data, zipping them together: > > for (cur, _, _), (next, _, _) in zip( > original_data[:-1], > original_data[1:] > ): > do_something(cur, next) This reminds me that I am a proponent of small dumb helper functions ;) I find def sliding_window(items): a, b = itertools.tee(items) next(b, None) return zip(a, b) dates = (date for date, _freq, _source in original_data) for from_date, to_date in sliding_window(dates): do_something(from_date, to_date) much more accessible. Plus, I can apply arbitrary improvements to the sliding_window() implementation or switch to a library version of that function without fear of messing things up. Likewise, should original_data become a sequence of namedtuples it is straightforward to propagate this change with dates = (item.date for item in original_data) > If your purpose for the "data" dict is to merely look up stats from > the next one, the whole batch of your original code can be replaced > with: > > for ( > (cur_dt, cur_freq, cur_source), > (next_dt, next_freq, next_source) > ) in zip(original_data[:-1], original_data[1:]): > # might need to do str(cur_dt) and str(next_dt) instead? > do_things_with(cur_dt, cur_freq, cur_source, > next_dt, next_freq, next_source) > > That eliminates the dict *and* the extra variable name. :-) Smileys are overused ;) Anyway, with namedtuples this ... would become for cur_item, next_item in zip(original_data, original_data[1:]): do_things_with(cur_item, next_item) Note that there's no need to slice the first argument as zip() ignores extra items.