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


Groups > comp.lang.python > #19987 > unrolled thread

Re: iterating over list with one mising value

Started byArnaud Delobelle <arnodel@gmail.com>
First post2012-02-07 21:37 +0000
Last post2012-02-07 21:37 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#19987 — Re: iterating over list with one mising value

FromArnaud Delobelle <arnodel@gmail.com>
Date2012-02-07 21:37 +0000
SubjectRe: iterating over list with one mising value
Message-ID<mailman.5518.1328650642.27778.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web