Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #11714 > unrolled thread
| Started by | Yingjie Lin <Yingjie.Lin@mssm.edu> |
|---|---|
| First post | 2011-08-17 16:22 -0400 |
| Last post | 2011-08-18 14:51 -0700 |
| Articles | 8 — 7 participants |
Back to article view | Back to comp.lang.python
pairwise combination of two lists Yingjie Lin <Yingjie.Lin@mssm.edu> - 2011-08-17 16:22 -0400
Re: pairwise combination of two lists Mel <mwilson@the-wire.com> - 2011-08-17 17:15 -0400
Re: pairwise combination of two lists Mel <mwilson@the-wire.com> - 2011-08-17 17:18 -0400
Re: pairwise combination of two lists Marc Christiansen <usenet@solar-empire.de> - 2011-08-17 23:43 +0200
Re: pairwise combination of two lists Luis M. González <luismgz@gmail.com> - 2011-08-17 16:27 -0700
Re: pairwise combination of two lists Paul Rubin <no.email@nospam.invalid> - 2011-08-18 00:18 -0700
Re: pairwise combination of two lists Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-08-18 09:30 +0200
Re: pairwise combination of two lists SigmundV <sigmundv@gmail.com> - 2011-08-18 14:51 -0700
| From | Yingjie Lin <Yingjie.Lin@mssm.edu> |
|---|---|
| Date | 2011-08-17 16:22 -0400 |
| Subject | pairwise combination of two lists |
| Message-ID | <mailman.146.1313614713.27778.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | Mel <mwilson@the-wire.com> |
|---|---|
| Date | 2011-08-17 17:15 -0400 |
| Message-ID | <j2hb1k$ntf$1@speranza.aioe.org> |
| In reply to | #11714 |
Yingjie Lin 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.
This seems to do it :
mwilson@tecumseth:~$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import itertools
>>> li1 = ['a', 'b']
>>> li2 = ['1', '2']
>>> map (lambda (x,y):x+y, list (itertools.product (li1, li2)))
['a1', 'a2', 'b1', 'b2']
Mel.
[toc] | [prev] | [next] | [standalone]
| From | Mel <mwilson@the-wire.com> |
|---|---|
| Date | 2011-08-17 17:18 -0400 |
| Message-ID | <j2hb7d$ntf$2@speranza.aioe.org> |
| In reply to | #11715 |
Mel wrote: > Yingjie Lin 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'] [ ... ] > This seems to do it : > > mwilson@tecumseth:~$ python > Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) > [GCC 4.4.3] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import itertools >>>> li1 = ['a', 'b'] >>>> li2 = ['1', '2'] >>>> map (lambda (x,y):x+y, list (itertools.product (li1, li2))) > ['a1', 'a2', 'b1', 'b2'] I have doubts about this in Python3, since tuple unpacking in a argument list isn't done there, and I don't think sum works on strings. Some other function can probably be made to work. Mel.
[toc] | [prev] | [next] | [standalone]
| From | Marc Christiansen <usenet@solar-empire.de> |
|---|---|
| Date | 2011-08-17 23:43 +0200 |
| Message-ID | <g9pth8-7co.ln1@pluto.solar-empire.de> |
| In reply to | #11714 |
Yingjie Lin <Yingjie.Lin@mssm.edu> 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.
Depending on your needs, we can offer you three solutions:
For our customers who want it all at once, but without any unneccessary
waste, the list expression:
[a + b for a, b in itertools.product(li1, li2)]
or if you don't need the whole list at once (especially interesting for
our customers with large lists), the generator expression (genexp):
(a + b for a, b in itertools.product(li1, li2))
and if you don't like the throwaway genexp and want something more
ecofriedly, we can present you a memory efficient, reusable solution, the
generator:
def combiner(li1, li2):
for a, b in itertools.product(li1, li2):
yield a + b
;)
HTH, Marc
[toc] | [prev] | [next] | [standalone]
| From | Luis M. González <luismgz@gmail.com> |
|---|---|
| Date | 2011-08-17 16:27 -0700 |
| Message-ID | <9ad7849a-021c-48c3-852a-1bbb41ee3179@x11g2000yqx.googlegroups.com> |
| In reply to | #11714 |
This is the easiest and most pythonic way (IMHO): >>>> l3 = [i+e for i in li1 for e in li2] >>>> l3 ['a1', 'a2', 'b1', 'b2'] Regards, Luis
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2011-08-18 00:18 -0700 |
| Message-ID | <7x1uwjdw6e.fsf@ruckus.brouhaha.com> |
| In reply to | #11714 |
Yingjie Lin <Yingjie.Lin@mssm.edu> writes: > li1 = ['a', 'b'] > li2 = ['1', '2'] > > and I wish to obtain a list like this > li3 = ['a1', 'a2', 'b1', 'b2'] from itertools import * li3 = list(chain.from_iterable(izip(li1,li2)))
[toc] | [prev] | [next] | [standalone]
| From | Alain Ketterlin <alain@dpt-info.u-strasbg.fr> |
|---|---|
| Date | 2011-08-18 09:30 +0200 |
| Message-ID | <87r54j5g7w.fsf@dpt-info.u-strasbg.fr> |
| In reply to | #11714 |
Yingjie Lin <Yingjie.Lin@mssm.edu> writes:
> 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.
It's not difficult to write your own:
def product(l1,l2):
for x1 in l1:
for x2 in l2:
yield x1+x2
use it like: for p in product(l1,l2) ... The call to product() produces
a generator, the cross product is never built in full, it should work on
long lists.
-- Alain.
[toc] | [prev] | [next] | [standalone]
| From | SigmundV <sigmundv@gmail.com> |
|---|---|
| Date | 2011-08-18 14:51 -0700 |
| Message-ID | <0da423ac-abc1-468d-bca7-8a745eae812b@y4g2000vbx.googlegroups.com> |
| In reply to | #11714 |
On Aug 17, 9:22 pm, Yingjie Lin <Yingjie....@mssm.edu> wrote:
> I found zip() but it only gives [('a', '1'), ('b', '2')], not exactly what I am looking for.
Yet, if you feed the zip into a list comprehension you get what you
want:
li3 = [''.join(l) for l in zip(li1,li2)]
Sigmund
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web