I want to replace each character in a string with all the alphabet and save the output in a list to work with it lately in a NLP project.
For example
var='abc'
Output expected
['bbc','cbc','dbc','ebc',...,'aac','acc','adc','aec',...,'aba','abb','abd','abe'...]
How can I do this with Python? Thanks
/Tried some codes but no luck
You may achieve this using below list comprehension expression as:
>>> import string
>>> var='abc'
# to get the string of all the lowercase alphabets v
>>> [var[:i]+s+var[i+1:]for i in range(len(var)) for s in string.lowercase if s!=var[i]]
which will return:
[
'bbc', 'cbc', 'dbc', 'ebc', 'fbc', 'gbc', 'hbc', 'ibc', 'jbc', 'kbc', 'lbc', 'mbc', 'nbc', 'obc', 'pbc', 'qbc', 'rbc', 'sbc', 'tbc', 'ubc', 'vbc', 'wbc', 'xbc', 'ybc', 'zbc',
'aac', 'acc', 'adc', 'aec', 'afc', 'agc', 'ahc', 'aic', 'ajc', 'akc', 'alc', 'amc', 'anc', 'aoc', 'apc', 'aqc', 'arc', 'asc', 'atc', 'auc', 'avc', 'awc', 'axc', 'ayc', 'azc', 'aba', 'abb', 'abd', 'abe', 'abf', 'abg', 'abh', 'abi', 'abj', 'abk',
'abl', 'abm', 'abn', 'abo', 'abp', 'abq', 'abr', 'abs', 'abt', 'abu', 'abv', 'abw', 'abx', 'aby', 'abz']