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


Groups > comp.lang.python > #60070

Re: zip list, variables

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'example:': 0.03; 'value,': 0.04; '[1,': 0.09; 'operand': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; '2],': 0.16; '3],': 0.16; '[])': 0.16; 'last)': 0.16; 'pythonic': 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; 'typeerror:': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'initial': 0.24; 'header:X -Complaints-To:1': 0.27; 'list:': 0.30; 'subject:list': 0.30; '"",': 0.31; 'file': 0.32; 'lists': 0.32; '(most': 0.33; "can't": 0.35; 'but': 0.35; 'doing': 0.36; 'list': 0.37; 'to:addr:python- list': 0.38; 'issue': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'most': 0.60; 'provide': 0.64; 'applying': 0.72; 'here...': 0.84; 'type(s)': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: zip list, variables
Date Wed, 20 Nov 2013 11:38:36 +0100
Organization None
References <31cfb6e8-aa7e-46c2-ae0b-18d0d66e7bed@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084ad45.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 <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2958.1384943908.18130.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1384943908 news.xs4all.nl 15955 [2001:888:2000:d::a6]:46694
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60070

Show key headers only | View raw


flebber wrote:

> If
> 
> c = map(sum, zip([1, 2, 3], [4, 5, 6]))
> 
> c
> Out[7]: [5, 7, 9]
> 
> why then can't I do this?
> 
> a = ([1, 2], [3, 4])
> 
> b = ([5, 6], [7, 8])
> 
> c = map(sum, zip(a, b))
> 
---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call
> last) <ipython-input-3-cc046c85514b> in <module>()
> ----> 1 c = map(sum, zip(a, b))
> 
> TypeError: unsupported operand type(s) for +: 'int' and 'list'
> 
> How can I do this legally?

You are obscuring the issue with your map-zippery. The initial value of 
sum() is 0, so if you want to "sum" lists you have to provide a start value, 
typically an empty list:

>>> sum([[1],[2]])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'list'
>>> sum([[1],[2]], [])
[1, 2]

Applying that to your example:

>>> def list_sum(items):
...     return sum(items, [])
... 
>>> map(list_sum, zip(a, b))
[[1, 2, 5, 6], [3, 4, 7, 8]]

Alternatively, reduce() does not require an initial value:

>>> map(functools.partial(reduce, operator.add), zip(a, b))
[[1, 2, 5, 6], [3, 4, 7, 8]]

But doing it with a list comprehension is the most pythonic solution here...

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


Thread

zip list, variables flebber <flebber.crue@gmail.com> - 2013-11-20 02:06 -0800
  Re: zip list, variables Peter Otten <__peter__@web.de> - 2013-11-20 11:38 +0100
  Re: zip list, variables Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-11-20 12:45 +0200
    Re: zip list, variables flebber <flebber.crue@gmail.com> - 2013-11-20 12:05 -0800
      Re: zip list, variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-21 01:03 +0000
      Re: zip list, variables Peter Otten <__peter__@web.de> - 2013-11-21 09:58 +0100

csiph-web