views:

85

answers:

1

Hi everyone,

I want following task to be done in Python without importing any modules.

My Code consists

Two List
---------
list1=['aun','2ab','acd','3aa']
list2=['ca3','ba2','dca','aa3']

Function
---------

Where it will:

  • Generates 2 items combination from list1
  • Generates 2 items combination from list2
  • Generates 2 items combination from list1 and list2

I don't need to print these all combinations of two items

But I want to pass all these 2 items combinations to further task and show results

 analysize R.. **ca3** .... and ... **2ab** // Combinations of two items from list1 and list2

 Print analysize
+5  A: 

Well, you already got the answer how to do it with itertools. If you want to do it without importing that module (for whatever reason...), you could still take a look at the docs and read the source:

def product(*args, **kwds):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = map(tuple, args) * kwds.get('repeat', 1)
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

and

def combinations(iterable, r):
    # combinations('ABCD', 2) --> AB AC AD BC BD CD
    # combinations(range(4), 3) --> 012 013 023 123
    pool = tuple(iterable)
    n = len(pool)
    if r > n:
        return
    indices = range(r)
    yield tuple(pool[i] for i in indices)
    while True:
        for i in reversed(range(r)):
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range(i+1, r):
            indices[j] = indices[j-1] + 1
        yield tuple(pool[i] for i in indices)
Tim Pietzcker
+1 the source be with you
knitti
Because why let someone else manage the source when you can?
Nick T
@Nick T: Yeah, those Python folk can't be trusted...
Tim Pietzcker
@ Tim Pietzcker- I have update my problems. Can you give me hint where can i pass all two list items values? I am little bit newbie to python
I must admit that I don't understand your problem. `product()` will yield tuples (pairs) of all combinations, so you can just pass those to whatever function you need. You might want to [read this](http://docs.python.org/tutorial/datastructures.html#tuples-and-sequences).
Tim Pietzcker