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


Groups > comp.lang.python > #108548

Re: More list building

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: More list building
Date Thu, 12 May 2016 11:08:04 +0200
Organization None
Lines 35
Message-ID <mailman.606.1463044097.32212.python-list@python.org> (permalink)
References <ngvph0$osu$1@dont-email.me> <9983c36a-7c06-45b6-9ab8-845eee5915d9@googlegroups.com> <nh1h5o$9ql$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de BTXcnMs3Hs9dckDuJhr4CQ2ZOiyL08hmuxUP+zBo2O8w==
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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'wednesday,': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'skip:p 40': 0.15; "'for',": 0.16; "'is',": 0.16; "'the',": 0.16; '2016': 0.16; 'dfs': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:More': 0.16; 'wrote:': 0.16; 'have:': 0.18; '>>>': 0.20; '(the': 0.22; 'seems': 0.23; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'header:X -Complaints-To:1': 0.26; 'skip:( 20': 0.28; 'omitted': 0.29; 'you?': 0.30; 'something': 0.35; 'but': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'email addr:gmail.com': 0.62; 'more': 0.63; 'utc-4,': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd907f.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
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>
X-Mailman-Original-Message-ID <nh1h5o$9ql$1@ger.gmane.org>
X-Mailman-Original-References <ngvph0$osu$1@dont-email.me> <9983c36a-7c06-45b6-9ab8-845eee5915d9@googlegroups.com>
Xref csiph.com comp.lang.python:108548

Show key headers only | View raw


louis.a.russ@gmail.com wrote:

> On Wednesday, May 11, 2016 at 1:22:09 PM UTC-4, DFS wrote:
>> Have:
>> p1 = ['Now', 'the', 'for', 'good']
>> p2 = ['is', 'time', 'all', 'men']
>> 
>> want
>> [('Now','is','the','time'), ('for','all','good','men')]
>> 
>> This works:
>> 
>> p = []
>> for i in xrange(0,len(p1),2):
>> p.insert(i,(p1[i],p2[i],p1[i+1],p2[i+1]))
>> 
>> 
>> But it seems clunky.
>> 
>> Better way(s)?
>> 
>> Thanks
> 
> Would this work for you? Or do you need something more general?
> t = list(zip(p1,p2))
> p = [t[0]+t[1],t[2]+t[3]]

This can be generalized as

>>> t = iter(zip(p1, p2))
>>> [a + b for a, b in zip(t, t)]
[('Now', 'is', 'the', 'time'), ('for', 'all', 'good', 'men')]

(The iter() call can be omitted in Python 3.)

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


Thread

More list building DFS <nospam@dfs.com> - 2016-05-11 13:21 -0400
  Re: More list building louis.a.russ@gmail.com - 2016-05-11 19:51 -0700
    Re: More list building Peter Otten <__peter__@web.de> - 2016-05-12 11:08 +0200
      Re: More list building DFS <nospam@dfs.com> - 2016-05-12 12:35 -0400
    Re: More list building DFS <nospam@dfs.com> - 2016-05-12 07:36 -0400
  Re: More list building marco.nawijn@colosso.nl - 2016-05-12 01:21 -0700
    Re: More list building DFS <nospam@dfs.com> - 2016-05-12 08:05 -0400

csiph-web