Re: pairwise combination of two lists

2011-08-18 Thread Paul Rubin
Yingjie Lin yingjie@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)))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pairwise combination of two lists

2011-08-18 Thread Alain Ketterlin
Yingjie Lin yingjie@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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pairwise combination of two lists

2011-08-18 Thread Ian Kelly
On Wed, Aug 17, 2011 at 4:22 PM, Yingjie Lin yingjie@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.
 I found zip() but it only gives [('a', '1'), ('b', '2')],  not exactly what I 
 am looking for.

Use the roundrobin recipe from the itertools documentation.

Cheers,
Ian
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pairwise combination of two lists

2011-08-18 Thread SigmundV
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
-- 
http://mail.python.org/mailman/listinfo/python-list


pairwise combination of two lists

2011-08-17 Thread Yingjie Lin
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







-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pairwise combination of two lists

2011-08-17 Thread Mel
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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pairwise combination of two lists

2011-08-17 Thread Mel
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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pairwise combination of two lists

2011-08-17 Thread Ned Deily
In article 98cc6556-11f3-4850-bd2b-30481b530...@mssm.edu,
 Yingjie Lin yingjie@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,
 n...@acm.org

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pairwise combination of two lists

2011-08-17 Thread Kev Dwyer
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


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pairwise combination of two lists

2011-08-17 Thread Marc Christiansen
Yingjie Lin yingjie@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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pairwise combination of two lists

2011-08-17 Thread Luis M . González
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pairwise combination of two lists

2011-08-17 Thread Gary Herron

On 08/17/2011 01:22 PM, 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

 li1 = ['a', 'b']
 li2 = ['1', '2']
 print [a+b   for a in li1   for b in li2]
['a1', 'a2', 'b1', 'b2']


Gary Herron
--
http://mail.python.org/mailman/listinfo/python-list