[Tutor] Calculating and returning possible combinations of elements from a given set
ZUXOXUS
zuxoxus at gmail.com
Wed Jul 28 01:07:28 CEST 2010
Oh, I think i got it:
>>> for prod in itertools.product('ABC', 'ABC'):
print(prod)
('A', 'A')
('A', 'B')
('A', 'C')
('B', 'A')
('B', 'B')
('B', 'C')
('C', 'A')
('C', 'B')
('C', 'C')
Thank you very much!!
2010/7/28 ZUXOXUS <zuxoxus at gmail.com>
> Mac,
>
> this is what I get:
>
> >>> for prod in itertools.product('ABC', 2):
> print(prod)
>
> Traceback (most recent call last):
> File "<pyshell#34>", line 1, in <module>
> for prod in itertools.product('ABC', 2):
> TypeError: 'int' object is not iterable
>
>
> hmm, what might be that 'int' object? 2?
>
>
> 2010/7/28 ZUXOXUS <zuxoxus at gmail.com>
>
> Sharp thanks, but:
>>
>> I try to reproduce the example from the table, but:
>>
>> >>> import itertools
>> >>> combinations('ABC', 2)
>> Traceback (most recent call last):
>> File "<pyshell#27>", line 1, in <module>
>> combinations('ABC', 2)
>> NameError: name 'combinations' is not defined
>> >>>
>>
>> If im not mistaken, it should return AB, AC, BA, etc.
>>
>> I'm using Python 3.1
>>
>>
>> 2010/7/28 Mark Lawrence <breamoreboy at yahoo.co.uk>
>>
>> On 27/07/2010 23:20, ZUXOXUS wrote:
>>>
>>>> Hi all pythoners
>>>>
>>>> I've got a probably easy to answer question.
>>>>
>>>> Say I've got a collections of strings, e.g.: 'man', 'bat', 'super',
>>>> 'ultra'.
>>>>
>>>> They are in a list, or in a sequence or whatever, say a bag of words
>>>>
>>>> And now I want to know how many couples I can do with them, and I want
>>>> the
>>>> program to show me the actual couples: 'manman', 'manbat', 'mansuper',
>>>> 'manultra', 'batbat', 'batman', 'batsuper', etc.
>>>>
>>>> But hey, why building up new words from just two strings? I also want to
>>>> know the possible combinations of three words, four words, and perhaps,
>>>> why
>>>> not, five words.
>>>>
>>>> So, is it easy to do?
>>>>
>>>> Sorry, I'm new in programing, and am probably far from being a
>>>> math-master
>>>>
>>>> I'm clueless, I think probably the code have some FOR I IN SEQUENCE...
>>>> but
>>>> then what? I don't know how to say: take every element and paste it to
>>>> another one from the bag, and with another one, and with another one,...
>>>>
>>>> If it's too complex, I dont need the whole code recipe, just need some
>>>> clues, or perhaps a useful link
>>>>
>>>> Thank you very much in advance!
>>>>
>>>> _______________________________________________
>>>> Tutor maillist - Tutor at python.org
>>>> To unsubscribe or change subscription options:
>>>> http://mail.python.org/mailman/listinfo/tutor
>>>>
>>>
>>> The lazy way.
>>>
>>> http://docs.python.org/library/itertools.html
>>> Look for combinations().
>>>
>>> HTH.
>>>
>>> Mark Lawrence.
>>>
>>>
>>>
>>> _______________________________________________
>>> Tutor maillist - Tutor at python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100728/4c449657/attachment-0001.html>
More information about the Tutor
mailing list