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


Groups > comp.lang.python > #11718

Re: pairwise combination of two lists

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!xlned.com!feeder5.xlned.com!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.010
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'subject:two': 0.07; 'python': 0.08; '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; 'users,': 0.13; '(1,': 0.16; '(x,': 0.16; 'handy': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; '>>>': 0.18; 'function': 0.27; 'import': 0.28; 'but...': 0.30; 'list': 0.32; "isn't": 0.33; 'there': 0.33; 'to:addr:python-list': 0.33; 'header:User-Agent:1': 0.34; 'header:X-Complaints-To:1': 0.35; 'thank': 0.35; '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; 'you.': 0.62; 'received:77': 0.63; 'wish': 0.69; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.71; 'reply-to:addr:gmail.com': 0.78; 'lists:': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Kev Dwyer <kevin.p.dwyer@gmail.com>
Subject Re: pairwise combination of two lists
Followup-To gmane.comp.python.general
Date Wed, 17 Aug 2011 22:33:34 +0100
References <98CC6556-11F3-4850-BD2B-30481B53042D@mssm.edu>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host cpc4-hari12-2-0-cust247.hari.cable.virginmedia.com
Mail-Copies-To kevin.p.dwyer@gmail.com
User-Agent KNode/4.4.10
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To kevin.p.dwyer@gmail.com
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.148.1313616838.27778.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1313616838 news.xs4all.nl 23858 [2001:888:2000:d::a6]:49849
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:11718

Followups directed to: gmane.comp.python.general

Show key headers only | View raw


Yingjie Lin wrote:

> Hi Python users,
> 
> 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.
> 
> Thank you.
> 
> 
> - Yingjie

Hello Yingjie,

This isn't exactly handy, but...

>>> import itertools
>>> a = ('a', 'b')
>>> b = (1, 2)
>>> [x + str(y) for (x, y) in itertools.product(*(a, b))]
['a1', 'a2', 'b1', 'b2']


Cheers,

Kev

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


Thread

Re: pairwise combination of two lists Kev Dwyer <kevin.p.dwyer@gmail.com> - 2011-08-17 22:33 +0100

csiph-web