views:

73

answers:

1

Hi everyone,

I have two list in a python

list1=['12aa','2a','c2']

list2=['2ac','c2a','1ac']

First- Finding combinations of each two item from list1.

Second- Finding combinations of each two item from list2.

Third- Finding combinations of each two items from list1 and list2

Fourth- Calculating each combinations total length

Advice and help in Python is appreciated.

Thank you

+3  A: 
import itertools as it

list1=['12aa','2a','c2']
list2=['2ac','c2a','1ac']

# First- Finding combinations of each two item from list1.
first = list(it.combinations(list1, 2))

# Second- Finding combinations of each two item from list2.
second = list(it.combinations(list2, 2))

# Third- Finding combinations of each two items from list1 and list2
third = list(it.product(list1, list2))

# Fourth- Calculating each combinations total length
for combination in first: # first, second, third
    print combination, len(''.join(combination))
eumiro
Actually, I want to produce combinations of each two item within a list and total characters in an individual combinations. Second, I want to produce combinations of each two items from list1 and list2 and total characters in an individual combinations.
Exactly as you edited in your question.
eumiro
Thank you so much, now I faced another problem. Suppose from our example list1 we got 3 combinations from three item. Now, I want to pass that individual combination into a function and want to know first item and second item for another operation but not messing up with previous problem
Sorry, I don't quite understand your last comment. Could you just add an edit to your original question together with an example?
eumiro
Is there any other way to do it without using combination? Because I figure out that when I try to run python file in other application terminal then it doesn't recognize combination(). Thank you
If you are using Python older than 2.6, please check here: http://docs.python.org/library/itertools.html#itertools.combinations for a replacement function for combinations.
eumiro