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


Groups > comp.lang.python > #11717

Re: pairwise combination of two lists

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.009
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'subject:two': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'from:addr:acm.org': 0.16; 'handy': 0.16; 'received:198.144': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'function': 0.27; 'import': 0.28; 'list': 0.32; 'received:198': 0.32; 'there': 0.33; 'to:addr :python-list': 0.33; 'header:User-Agent:1': 0.34; 'header:X -Complaints-To:1': 0.35; 'charset:us-ascii': 0.36; 'subject:lists': 0.36; 'but': 0.37; 'especially': 0.37; 'two': 0.37; 'received:org': 0.38; 'subject:: ': 0.39; 'header:Mime- Version:1': 0.39; 'to:addr:python.org': 0.39; 'wish': 0.69; 'article': 0.76; 'lists:': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ned Deily <nad@acm.org>
Subject Re: pairwise combination of two lists
Date Wed, 17 Aug 2011 14:33:17 -0700
References <98CC6556-11F3-4850-BD2B-30481B53042D@mssm.edu>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host baybryj.net
User-Agent MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)
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.147.1313616814.27778.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1313616814 news.xs4all.nl 23840 [2001:888:2000:d::a6]:49590
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:11717

Show key headers only | View raw


In article <98CC6556-11F3-4850-BD2B-30481B53042D@mssm.edu>,
 Yingjie Lin <Yingjie.Lin@mssm.edu> wrote:
> I have two lists: 
> 
> li1 = ['a', 'b']
> li2 = ['1', '2']
> 
> and I wish to obtain a list like this
> 
> li3 = ['a1', 'a2', 'b1', 'b2']
> 
> Is there a handy and efficient function to do this, especially when li1 and 
> li2 are long lists.
> I found zip() but it only gives [('a', '1'), ('b', '2')],  not exactly what I 
> am looking for.

>>> from itertools import product
>>> li1 = ['a', 'b']
>>> li2 = ['1', '2']
>>> li3 = list("".join(x) for x in product(li1, li2))
>>> li3
['a1', 'a2', 'b1', 'b2']

-- 
 Ned Deily,
 nad@acm.org

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


Thread

Re: pairwise combination of two lists Ned Deily <nad@acm.org> - 2011-08-17 14:33 -0700

csiph-web