Related Articles

# Python – Character Replacement Combination

• Last Updated : 29 Aug, 2020

Given a String and dictionary with characters mapped to replacement characters values list,  construct all possible strings after replacing present characters with mapped values.

Input : test_str = “geeks”, test_dict = {‘s’ : [‘1’, ‘5’], ‘k’ : [‘3’]}
Output : [‘gee31’, ‘geek1’, ‘gee35’, ‘geek5’, ‘gee3s’, ‘geeks’]
Explanation : All possible replacement of strings, e.g in ‘gee35’, k is replaced by ‘3’ and s is replaced by ‘5’.

Input : test_str = “geeks”, test_dict = {‘s’ : [‘1’], ‘k’ : [‘3’]}
Output : [‘gee31’, ‘geek1’, ‘gee3s’, ‘geeks’]
Explanation : All possible replacement of strings, e.g in ‘gee31’, k is replaced by ‘3’ and s is replaced by ‘1’.

Method : Using zip() + list comprehension + replace() + product()

The combination of above functions can be used to solve this problem. In this, we extract all the combination characters using product and pair one at a time using zip(), replacement using replace().

## Python3

 `# Python3 code to demonstrate working of ``# Character Replacement Combination``# Using zip() + list comprehension + replace() + product()``from` `itertools ``import` `product`` ` `# initializing string``test_str ``=` `"geeks"`` ` `# printing original string``print``(``"The original string is : "` `+` `str``(test_str))`` ` `# initializing dictionary``test_dict ``=` `{``'s'` `: [``'1'``, ``'2'``], ``'k'` `: [``'3'``]}`` ` `# adding original character to possible characters ``for` `key ``in` `test_dict.keys():``    ``if` `key ``not` `in` `test_dict[key]:``        ``test_dict[key].append(key)`` ` `res ``=` `[]`` ` `# constructing all possible combination of values using product``# mapping using zip()``for` `sub ``in` `[``zip``(test_dict.keys(), ``chr``) ``for` `chr` `in` `product(``*``test_dict.values())]:``    ``temp ``=` `test_str``    ``for` `repls ``in` `sub:``         ` `        ``# replacing all elements at once using * operator``        ``temp ``=` `temp.replace(``*``repls)``    ``res.append(temp)`` ` `# printing result ``print``(``"All combinations : "` `+` `str``(res)) `
Output
```The original string is : geeks
All combinations : ['gee31', 'geek1', 'gee32', 'geek2', 'gee3s', 'geeks']
```

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.6039866,"math_prob":0.8029337,"size":2264,"snap":"2021-43-2021-49","text_gpt3_token_len":594,"char_repetition_ratio":0.13451327,"word_repetition_ratio":0.1396648,"special_character_ratio":0.2941696,"punctuation_ratio":0.17631578,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9605154,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-24T03:47:52Z\",\"WARC-Record-ID\":\"<urn:uuid:bc87631b-96b3-4ba9-a8e6-cb56ed8701b7>\",\"Content-Length\":\"108963\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ee3dbaae-2e04-4721-bfe7-7a50894f7b5f>\",\"WARC-Concurrent-To\":\"<urn:uuid:d23c0f7c-cdec-469a-8ca0-2f259d9023ac>\",\"WARC-IP-Address\":\"23.222.4.84\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/python-character-replacement-combination/?ref=rp\",\"WARC-Payload-Digest\":\"sha1:D3S5ZFJCEBQ57OO7BO22WXLOB7EA4IQO\",\"WARC-Block-Digest\":\"sha1:Q7TWMBYLIMRDPMHNHE3YR2H6TQVQJATJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585837.82_warc_CC-MAIN-20211024015104-20211024045104-00626.warc.gz\"}"}