Alex Parker
Alex Parker

Reputation: 1583

Append itertools.product() results to variable in Python

I am trying to append every possible two-digit combination to the end of each string in a list.

The strings are each eight characters, and the digits should replace the seventh and eighth characters.

I am using itertools.product() to generate these two-digit combos, but I am not sure how to then append these combinations to strings. I tried using join(), but that sandwiches the string between each of the two digits.

My next attempt is below but doesn't work because you cannot concatenate 'str' and 'itertools.product' objects.

for p in passwords:
    candidates += list(p[:6] + itertools.product(string.digits, string.digits))

So, passwords looks like this

['american', 'japanese']

and the output should be

['americ00', 'americ01', 'americ02', …, 'japane98', 'japane99']

Upvotes: 1

Views: 1544

Answers (2)

Ryan Haining
Ryan Haining

Reputation: 36782

Since you're just counting, the product isn't necessary This could be abbreviated to:

 lst = ['american', 'japanese']
 candidates = ['{}{:02d}'.format(e[:6], i) for e in lst for i in range(100)]

Which is alanalgous to the loop

candidates = []
for e in lst:
    for i in range(100):
        candidates.append('{}{:02d}'.format(e[:6], i))

If really want product for one reason or another:

['{}{}{}'.format(e[:6], i, j) for e in lst for i, j in itertools.product(string.digits, string.digits)]

This can also generalize to a product with more arguments

[e[:6] + ''.join(pr) for e in lst for pr in itertools.product(string.digits, string.digits, string.digits)]

Upvotes: 3

Anshul Goyal
Anshul Goyal

Reputation: 76837

If I am understanding this right, you want to create a list of candidates for each password. in that case, you could use something like below, where the replacements are calculated only once, and all the candidates are comma separated values:

import itertools
import string

candidates = []
replacements = ['%s%s' % (x, y) for x, y in itertools.product(string.digits, string.digits)]

list_of_l = ['american', 'japanese']
for l in list_of_l:
    candidates += [l[:6] + repl for repl in replacements]

>>> print candidates
['americ00', 'americ01', 'americ02', 'americ03', 'americ04', 'americ05', 'americ06', 'americ07', 'americ08', 'americ09', 'americ10',
'americ11', 'americ12', 'americ13', 'americ14', 'americ15', 'americ16', 'americ17', 'americ18', 'americ19', 'americ20', 'americ21',
'americ22', 'americ23', 'americ24', 'americ25', 'americ26', 'americ27', 'americ28', 'americ29', 'americ30', 'americ31', 'americ32',
'americ33', 'americ34', 'americ35', 'americ36', 'americ37', 'americ38', 'americ39', 'americ40', 'americ41', 'americ42', 'americ43',
'americ44', 'americ45', 'americ46', 'americ47', 'americ48', 'americ49', 'americ50', 'americ51', 'americ52', 'americ53', 'americ54',
'americ55', 'americ56', 'americ57', 'americ58', 'americ59', 'americ60', 'americ61', 'americ62', 'americ63', 'americ64', 'americ65',
'americ66', 'americ67', 'americ68', 'americ69', 'americ70', 'americ71', 'americ72', 'americ73', 'americ74', 'americ75', 'americ76',
'americ77', 'americ78', 'americ79', 'americ80', 'americ81', 'americ82', 'americ83', 'americ84', 'americ85', 'americ86', 'americ87',
'americ88', 'americ89', 'americ90', 'americ91', 'americ92', 'americ93', 'americ94', 'americ95', 'americ96', 'americ97', 'americ98',
'americ99', 'japane00', 'japane01', 'japane02', 'japane03', 'japane04', 'japane05', 'japane06', 'japane07', 'japane08', 'japane09',
'japane10', 'japane11', 'japane12', 'japane13', 'japane14', 'japane15', 'japane16', 'japane17', 'japane18', 'japane19', 'japane20',
'japane21', 'japane22', 'japane23', 'japane24', 'japane25', 'japane26', 'japane27', 'japane28', 'japane29', 'japane30', 'japane31',
'japane32', 'japane33', 'japane34', 'japane35', 'japane36', 'japane37', 'japane38', 'japane39', 'japane40', 'japane41', 'japane42',
'japane43', 'japane44', 'japane45', 'japane46', 'japane47', 'japane48', 'japane49', 'japane50', 'japane51', 'japane52', 'japane53',
'japane54', 'japane55', 'japane56', 'japane57', 'japane58', 'japane59', 'japane60', 'japane61', 'japane62', 'japane63', 'japane64',
'japane65', 'japane66', 'japane67', 'japane68', 'japane69', 'japane70', 'japane71', 'japane72', 'japane73', 'japane74', 'japane75',
'japane76', 'japane77', 'japane78', 'japane79', 'japane80', 'japane81', 'japane82', 'japane83', 'japane84', 'japane85', 'japane86',
'japane87', 'japane88', 'japane89', 'japane90', 'japane91', 'japane92', 'japane93', 'japane94', 'japane95', 'japane96', 'japane97',
'japane98', 'japane99']

Note that list is a python builtin, so it is not advised to use it as a variable name and hence I've used list_of_l to differentiate.

Upvotes: 1

Related Questions