Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
list of all possible values
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
David Gibb  
View profile  
 More options Jul 13, 12:21 pm
Newsgroups: comp.lang.python
From: David Gibb <deg...@gmail.com>
Date: Mon, 13 Jul 2009 12:21:37 -0400
Local: Mon, Jul 13 2009 12:21 pm
Subject: list of all possible values
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, 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:

vals = ['a', 'b', 'c']

def foo(length):
    if length <=0:
        return []
    if length == 1:
        return [[x] for x in vals]
    else:
        return [x + [y] for x in foo(length - 1) for y in vals]

print foo(3)

Thanks,
David


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Chase  
View profile  
 More options Jul 13, 12:33 pm
Newsgroups: comp.lang.python
From: Tim Chase <python.l...@tim.thechases.com>
Date: Mon, 13 Jul 2009 11:33:20 -0500
Local: Mon, Jul 13 2009 12:33 pm
Subject: Re: list of all possible values

Sounds like you want one of the combinitoric generators found in
itertools[1] -- in this case, the itertools.product() does what
you describe.  According to the docs, it was added in 2.6 so if
you're running an older version, you'd have to back-port it.

-tkc

[1]
http://docs.python.org/library/itertools.html#itertools.product


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bearophile  
View profile  
 More options Jul 13, 12:35 pm
Newsgroups: comp.lang.python
From: Bearophile <bearophileH...@lycos.com>
Date: Mon, 13 Jul 2009 09:35:49 -0700 (PDT)
Local: Mon, Jul 13 2009 12:35 pm
Subject: Re: list of all possible values
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'), ('c', 'b'), ('c', 'c')]

Bye,
bearophile


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Emile van Sebille  
View profile  
 More options Jul 13, 1:58 pm
Newsgroups: comp.lang.python
From: Emile van Sebille <em...@fenx.com>
Date: Mon, 13 Jul 2009 10:58:25 -0700
Local: Mon, Jul 13 2009 1:58 pm
Subject: Re: list of all possible values
On 7/13/2009 9:33 AM Tim Chase said...

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']
 >>>

Emile


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Gibb  
View profile  
 More options Jul 13, 2:15 pm
Newsgroups: comp.lang.python
From: David Gibb <deg...@gmail.com>
Date: Mon, 13 Jul 2009 14:15:24 -0400
Local: Mon, Jul 13 2009 2:15 pm
Subject: Re: list of all possible values

> 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
list length.

Thanks for the itertools suggestion, guys. I was hoping to improve my
list-comprehension-fu, but that module was the next best thing.

David


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andreas Tawn  
View profile  
 More options Jul 13, 12:51 pm
Newsgroups: comp.lang.python
From: "Andreas Tawn" <andreas.t...@ubisoft.com>
Date: Mon, 13 Jul 2009 18:51:07 +0200
Local: Mon, Jul 13 2009 12:51 pm
Subject: RE: list of all possible values

> 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'), ('c', 'b'), ('c', 'c')]

> Bye,
> bearophile

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.

Cheers,

Drea


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Gibb  
View profile  
 More options Jul 16, 12:15 pm
Newsgroups: comp.lang.python
From: David Gibb <deg...@gmail.com>
Date: Thu, 16 Jul 2009 12:15:46 -0400
Local: Thurs, Jul 16 2009 12:15 pm
Subject: Re: list of all possible values

> 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 missing my point. I should have explained better.

The advantage that bearophile's version is generic with respect to the
number of elements in each combination. To go from 2 element pairs
(e.g. ('a', 'c')) to 5 element pairs (e.g. ('a', 'c', 'b', 'b', 'e'))
requires only a change in a parameter passed to itertools.

I don't see how you would do that with list comprehensions. You're
example works nicely with 2 element pairs, but it seems to me like
you'd need to recode it if you wanted to change it to 5 element pairs.

Am I wrong about that? Can you think of a way to write a function
that, using list comprehensions, takes a list of values and the size
of each combination, and returns the len(list)**(combination size)
possible combinations using those values?

Thanks again,
David


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andreas Tawn  
View profile  
 More options Jul 16, 12:49 pm
Newsgroups: comp.lang.python
From: "Andreas Tawn" <andreas.t...@ubisoft.com>
Date: Thu, 16 Jul 2009 18:49:11 +0200
Local: Thurs, Jul 16 2009 12:49 pm
Subject: RE: list of all possible values

David,

I think my post got caught in the nebulous email eddys and seems to have
taken 16 hours to arrive on the list. It was meant to be a reply to your
first post, not your second.

Having said that, I think I missed the point of that post too ;o)

Maybe someone smarter than me can come up with a way to dynamically nest
the fors in a list comprehension, but I certainly can't do it.

Sorry for the confusion.

Cheers,

Drea


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mensanator  
View profile  
 More options Jul 16, 4:23 pm
Newsgroups: comp.lang.python
From: Mensanator <mensana...@aol.com>
Date: Thu, 16 Jul 2009 13:23:38 -0700 (PDT)
Local: Thurs, Jul 16 2009 4:23 pm
Subject: Re: list of all possible values
On Jul 16, 11:49 am, "Andreas Tawn" <andreas.t...@ubisoft.com> wrote:

Well, I don't use list comprehension, but you can certainly
make dynamic for loops. Not that this is a replacement for
itertools (I wrote it before itertools had the ability to do
all this).

def ooloop6(a, n, perm=True, repl=True):
    if (not repl) and (n>len(a)): return
    r0 = range(n)
    r1 = r0[1:]
    if perm and repl:                          # ok
        v = ','.join(['c%s' % i for i in r0])
        f = ' '.join(['for c%s in a' % i for i in r0])
        e = ''.join(["p = [''.join((",v,")) ",f,"]"])
        exec e
        return p
    if (not perm) and repl:                    # ok
        v = ','.join(['c%s' % i for i in r0])
        f = ' '.join(['for c%s in a' % i for i in r0])
        i = ' and '.join(['(c%s>=c%s)' % (j,j-1) for j in r1])
        e = ''.join(["p = [''.join((",v,")) ",f," if ",i,"]"])
        exec e
        return p
    if perm and (not repl):                    # ok
        v = ','.join(['c%s' % i for i in r0])
        f = ' '.join(['for c%s in a' % i for i in r0])
        i = ' and '.join([' and '.join(['(c%s!=c%s)' % (j,k) for k in
range(j)]) for j in r1])
        e = ''.join(["p = [''.join((",v,")) ",f," if ",i,"]"])
        exec e
        return p
    if (not perm) and (not repl):              # ok
        v = ','.join(['c%s' % i for i in r0])
        f = ' '.join(['for c%s in a' % i for i in r0])
        i = ' and '.join(['(c%s>c%s)' % (j,j-1) for j in r1])
        e = ''.join(["p = [''.join((",v,")) ",f," if ",i,"]"])
        exec e
        return p

print '0123456789 taken 2 at a time'

p = ooloop6('0123456789',2,True,True)
print
print 'Permutations with    Replacement: %6d' % (len(p))
print p

p = ooloop6('0123456789',2,True,False)
print
print 'Permutations without Replacement: %6d' % (len(p))
print p

p = ooloop6('0123456789',2,False,True)
print
print 'Combinations with    Replacement: %6d' % (len(p))
print p

p = ooloop6('0123456789',2,False,False)
print
print 'Combinations without Replacement: %6d' % (len(p))
print p

print

print '0123456789 taken 3 at a time'

p = ooloop6('0123456789',3,True,True)
print
print 'Permutations with    Replacement: %6d' % (len(p))
print p

p = ooloop6('0123456789',3,True,False)
print
print 'Permutations without Replacement: %6d' % (len(p))
print p

p = ooloop6('0123456789',3,False,True)
print
print 'Combinations with    Replacement: %6d' % (len(p))
print p

p = ooloop6('0123456789',3,False,False)
print
print 'Combinations without Replacement: %6d' % (len(p))
print p

0123456789 taken 2 at a time

Permutations with    Replacement:    100
['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10',
'11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21',
'22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32',
'33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43',
'44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54',
'55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65',
'66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76',
'77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87',
'88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98',
'99']

Permutations without Replacement:     90
['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '12',
'13', '14', '15', '16', '17', '18', '19', '20', '21', '23', '24',
'25', '26', '27', '28', '29', '30', '31', '32', '34', '35', '36',
'37', '38', '39', '40', '41', '42', '43', '45', '46', '47', '48',
'49', '50', '51', '52', '53', '54', '56', '57', '58', '59', '60',
'61', '62', '63', '64', '65', '67', '68', '69', '70', '71', '72',
'73', '74', '75', '76', '78', '79', '80', '81', '82', '83', '84',
'85', '86', '87', '89', '90', '91', '92', '93', '94', '95', '96',
'97', '98']

Combinations with    Replacement:     55
['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '11',
'12', '13', '14', '15', '16', '17', '18', '19', '22', '23', '24',
'25', '26', '27', '28', '29', '33', '34', '35', '36', '37', '38',
'39', '44', '45', '46', '47', '48', '49', '55', '56', '57', '58',
'59', '66', '67', '68', '69', '77', '78', '79', '88', '89', '99']

Combinations without Replacement:     45
['01', '02', '03', '04', '05', '06', '07', '08', '09', '12', '13',
'14', '15', '16', '17', '18', '19', '23', '24', '25', '26', '27',
'28', '29', '34', '35', '36', '37', '38', '39', '45', '46', '47',
'48', '49', '56', '57', '58', '59', '67', '68', '69', '78', '79',
'89']

0123456789 taken 3 at a time

Permutations with    Replacement:   1000
['000', '001', '002', '003', '004', '005', '006', '007', '008', '009',
'010', '011', '012', '013', '014', '015', '016', '017', '018', '019',
'020', '021', '022', '023', '024', '025', '026', '027', '028', '029',
'030', '031', '032', '033', '034', '035', '036', '037', '038', '039',
'040', '041', '042', '043', '044', '045', '046', '047', '048', '049',
'050', '051', '052', '053', '054', '055', '056', '057', '058', '059',
'060', '061', '062', '063', '064', '065', '066', '067', '068', '069',
'070', '071', '072', '073', '074', '075', '076', '077', '078', '079',
'080', '081', '082', '083', '084', '085', '086', '087', '088', '089',
'090', '091', '092', '093', '094', '095', '096', '097', '098', '099',
'100', '101', '102', '103', '104', '105', '106', '107', '108', '109',
'110', '111', '112', '113', '114', '115', '116', '117', '118', '119',
'120', '121', '122', '123', '124', '125', '126', '127', '128', '129',
'130', '131', '132', '133', '134', '135', '136', '137', '138', '139',
'140', '141', '142', '143', '144', '145', '146', '147', '148', '149',
'150', '151', '152', '153', '154', '155', '156', '157', '158', '159',
'160', '161', '162', '163', '164', '165', '166', '167', '168', '169',
'170', '171', '172', '173', '174', '175', '176', '177', '178', '179',
'180', '181', '182', '183', '184', '185', '186', '187', '188', '189',
'190', '191', '192', '193', '194', '195', '196', '197', '198', '199',
'200', '201', '202', '203', '204', '205', '206', '207', '208', '209',
'210', '211', '212', '213', '214', '215', '216', '217', '218', '219',
'220', '221', '222', '223', '224', '225', '226', '227', '228', '229',
'230', '231', '232', '233', '234', '235', '236', '237', '238', '239',
'240', '241', '242', '243', '244', '245', '246', '247', '248', '249',
'250', '251', '252', '253', '254', '255', '256', '257', '258', '259',
'260', '261', '262', '263', '264', '265', '266', '267', '268', '269',
'270', '271', '272', '273', '274', '275', '276', '277', '278', '279',
'280', '281', '282', '283', '284', '285', '286', '287', '288', '289',
'290', '291', '292', '293', '294', '295', '296', '297', '298', '299',
'300', '301', '302', '303', '304', '305', '306', '307', '308', '309',
'310', '311', '312', '313', '314', '315', '316', '317', '318', '319',
'320', '321', '322', '323', '324', '325', '326', '327', '328', '329',
'330', '331', '332', '333', '334', '335', '336', '337', '338', '339',
'340', '341', '342', '343', '344', '345', '346', '347', '348', '349',
'350', '351', '352', '353', '354', '355', '356', '357', '358', '359',
'360', '361', '362', '363', '364', '365', '366', '367', '368', '369',
'370', '371', '372', '373', '374', '375', '376', '377', '378', '379',
'380', '381', '382', '383', '384', '385', '386', '387', '388', '389',
'390', '391', '392', '393', '394', '395', '396', '397', '398', '399',
'400', '401', '402', '403', '404', '405', '406', '407', '408', '409',
'410', '411', '412', '413', '414', '415', '416', '417', '418', '419',
'420', '421', '422', '423', '424', '425', '426', '427', '428', '429',
'430', '431', '432', '433', '434', '435', '436', '437', '438', '439',
'440', '441', '442', '443', '444', '445', '446', '447', '448', '449',
'450', '451', '452', '453', '454', '455', '456', '457', '458', '459',
'460', '461', '462', '463', '464', '465', '466', '467', '468', '469',
'470', '471', '472', '473', '474', '475', '476', '477', '478', '479',
'480', '481', '482', '483', '484', '485', '486', '487', '488', '489',
'490', '491', '492', '493', '494', '495', '496', '497', '498', '499',
'500', '501', '502', '503', '504', '505', '506', '507', '508', '509',
'510', '511', '512', '513', '514', '515', '516', '517', '518', '519',
'520', '521', '522', '523', '524', '525', '526', '527', '528', '529',
'530', '531', '532', '533', '534', '535', '536', '537', '538', '539',
'540', '541', '542', '543', '544', '545', '546', '547', '548',
...

read more »


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google