Given a string list and list of numbers, the task is to write a Python program to generate all possible strings by repeating each character of each string by each number in the list.
Input : test_list = ["gfg", "is", "best"], rep_list = [3, 5, 2]
Output : ['gggfffggg', 'iiisss', 'bbbeeesssttt', 'gggggfffffggggg', 'iiiiisssss', 'bbbbbeeeeesssssttttt', 'ggffgg', 'iiss', 'bbeesstt']
Explanation : Each element of 'gfg' is repeated 3, 5 and 2 times to output different strings.
Input : test_list = ["gfg", "is", "best"], rep_list = [3, 1, 2]
Output : ['gggfffggg', 'iiisss', 'bbbeeesssttt', 'gfg', 'is', 'best', 'ggffgg', 'iiss', 'bbeesstt']
Explanation : Each element of 'gfg' is repeated 3, 1 and 2 times to output different strings.
In this, the task of constructing each string is done using join(). The * operator performs the task of creating multiple character occurrences. The nested loop is used to combine each number with each string.
OutputThe original list is : ['gfg', 'is', 'best']
All repetition combinations strings : ['gggfffggg', 'iiisss', 'bbbeeesssttt', 'gggggfffffggggg', 'iiiiisssss', 'bbbbbeeeeesssssttttt', 'ggffgg', 'iiss', 'bbeesstt']
The nested loop for generating pairs is avoiding in this method by the use of the product() method. Rest all the functionality remains same as the above method.
OutputThe original list is : ['gfg', 'is', 'best']
All repetition combinations strings : ['gggfffggg', 'gggggfffffggggg', 'ggffgg', 'iiisss', 'iiiiisssss', 'iiss', 'bbbeeesssttt', 'bbbbbeeeeesssssttttt', 'bbeesstt']