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


Groups > comp.lang.python > #19987

Re: iterating over list with one mising value

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <arnodel@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'below)': 0.05; 'transform': 0.07; 'iterate': 0.09; 'truncate': 0.09; 'valueerror:': 0.09; 'def': 0.13; 'received:209.85.214.174': 0.13; '(key,': 0.16; '1),': 0.16; '3),': 0.16; 'pad': 0.16; 'traceback.': 0.16; 'unpack': 0.16; 'unpacking': 0.16; 'cc:addr :python-list': 0.16; 'this:': 0.16; 'wrote:': 0.18; 'help.': 0.19; 'subject:list': 0.21; 'header:In-Reply-To:1': 0.22; 'produces': 0.23; 'guess': 0.26; 'code': 0.26; 'work.': 0.27; 'work,': 0.28; 'cc:addr:gmail.com': 0.28; '(see': 0.28; 'message- id:@mail.gmail.com': 0.29; 'problem': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'fails.': 0.30; 'key,': 0.30; 'value)': 0.30; 'error': 0.30; 'cc:2**2': 0.31; 'thanks': 0.32; 'error.': 0.32; 'list': 0.32; 'loop': 0.34; 'rather': 0.34; 'running': 0.34; 'something': 0.35; 'received:209.85.214': 0.36; 'received:google.com': 0.37; 'subject:with': 0.37; 'received:209.85': 0.38; 'doing': 0.38; 'should': 0.38; 'data': 0.38; 'received:209': 0.39; 'more': 0.61; 'your': 0.61; 'provided': 0.62; 'below': 0.62; 'subject:one': 0.77; 'to:addr:yahoo.com': 0.83; 'subject:over': 0.84; 'subject:value': 0.84; 'cc:no real name:2**2': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=bAbSWGHHTsqo7B4beUxjj0vEvLBeauftKUEMIsZVinI=; b=Arw9KG7yqNFEwpu05W1kqqesT/roCXUnUCx11ZPtcjiGBbpEOt2CPUzQK2+gjTT6cv iZAwlW3/G/HWJd4sz6up+24CBfEL8k0l+5LTyYWjCuCx3cZQ8XxkvVy/jh3NOmlOxmj3 26B5qrp4DD7MyQqz5gzNdpLxXNv4uwq4DUrnI=
MIME-Version 1.0
In-Reply-To <1328646189.84221.YahooMailClassic@web161206.mail.bf1.yahoo.com>
References <1328646189.84221.YahooMailClassic@web161206.mail.bf1.yahoo.com>
Date Tue, 7 Feb 2012 21:37:20 +0000
Subject Re: iterating over list with one mising value
From Arnaud Delobelle <arnodel@gmail.com>
To Sammy Danso <samdansobe@yahoo.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org, d@davea.name
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5518.1328650642.27778.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328650642 news.xs4all.nl 6885 [2001:888:2000:d::a6]:32829
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19987

Show key headers only | View raw


On 7 February 2012 20:23, Sammy Danso <samdansobe@yahoo.com> wrote:
>
> Hi Expert,
> Thanks for your responses and help. thought I should provide more information for clarity.

Please don't top-post.

> Please find the error message below for more information
>
>    for (key, value) in wordFreq2:
> ValueError: need more than 1 value to unpack
>
> this is a sample of my data
>
> ['with', 3, 'which', 1, [...] , 3, 'other']
>
> What I would like to do is to pad the last value 'other' with a default so I can iterate sucessfully

* It would be better if you provided the code that produces the error,
rather than just the error.  This would allow others to diagnose your
problem without having to guess what you're really doing (see my guess
below)

* Also, don't truncate the traceback.

My guess: you're running a loop like this, where each item is unpacked:

    for (key, value) in wordFreq2:
        print key, value

on data like that:

    wordFreq2 = ['with', 3, 'which', 1, 'were', 2, 'well', 1]

Your list is flat so the unpacking fails.  For it to work, you need
your list to be of the form:

    wordFreq2 = [('with', 3), ('which', 1), ('were', 2), ('well', 1)]

Then it will work.  The quickest way to transform your list to the
required form is something like this:

    def pairs(seq, fillvalue):
        it = iter(seq)
        return list(izip_longest(it, it, fillvalue=fillvalue)

so you can do:

    word_freq_pairs = pairs(wordFreq2)

-- 
Arnaud

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


Thread

Re: iterating over list with one mising value Arnaud Delobelle <arnodel@gmail.com> - 2012-02-07 21:37 +0000

csiph-web