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
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)
精彩评论