On Jul 16, 11:49 am, "Andreas Tawn" wrote:
> > > Certainly possible with list comprehensions.
>
> > a = "abc"
> > [(x, y) for x in a for y in a]
> > > [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b',
> 'c'),
> > > ('c', 'a'), ('c', 'b'), ('c', 'c')]
>
> > > But I like b
> > Certainly possible with list comprehensions.
> >
> a = "abc"
> [(x, y) for x in a for y in a]
> > [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b',
'c'),
> > ('c', 'a'), ('c', 'b'), ('c', 'c')]
> >
> > But I like bearophile's version better.
> >
>
> Andreas,
>
> Tha
> Certainly possible with list comprehensions.
>
a = "abc"
[(x, y) for x in a for y in a]
> [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'),
> ('c', 'a'), ('c', 'b'), ('c', 'c')]
>
> But I like bearophile's version better.
>
Andreas,
Thanks, but I think you were
> David Gibb:
> > For example: if my values are ['a', 'b', 'c'], then all possible
lists
> > of length 2 would be: aa, ab, ac, ba, bb, bc, ca, cb, cc.
>
> >>> from itertools import product
> >>> list(product("abc", repeat=2))
> [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b',
> '
> Or on systems with list comps try:
>
V='abc'
['%s%s'%(ii,jj) for ii in V for jj in V]
> ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
Yeah, except that the length here is hard-coded. There's no way (as
far as I can tell, at least), to make this generic with respect to
lis
On 7/13/2009 9:33 AM Tim Chase said...
For example: if my values are ['a', 'b', 'c'], then all possible lists
of length 2 would be: aa, ab, ac, ba, bb, bc, ca, cb, cc.
I created a recursive program to do it, but I was wondering if there
was a better way of doing it (possibly with list comprehens
David Gibb:
> For example: if my values are ['a', 'b', 'c'], then all possible lists
> of length 2 would be: aa, ab, ac, ba, bb, bc, ca, cb, cc.
>>> from itertools import product
>>> list(product("abc", repeat=2))
[('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b',
'c'), ('c', 'a'),
For example: if my values are ['a', 'b', 'c'], then all possible lists
of length 2 would be: aa, ab, ac, ba, bb, bc, ca, cb, cc.
I created a recursive program to do it, but I was wondering if there
was a better way of doing it (possibly with list comprehensions).
Here's my recursive version:
va
Hi guys.
I was thinking about a problem I had: suppose I have a list of
possible values. I want to to have a list of all possible lists of
length n whose values are in that original list.
For example: if my values are ['a', 'b', 'c'], then all possible lists
of length 2 would be: aa, ab, ac, ba,