Reputation:
The question has racked my brains
There are 26 underscores presenting English alphabet in-sequence. means that letter a,b and g should be substituted by the letter k, j and r respectively, while all the other letters are not substituted.
how do I do like this? How can python detect each underscore = each English alphabet?
I thought I could use str.replace to do this
but it's more difficult than I thought.
thanks
Upvotes: 0
Views: 264
Reputation: 12669
Let's solve your issue step by step without making it too much complicated:
First step :
So first step is data gathering which user providing or you already have :
Suppose you have one list of a-z alphabets and other list have replaced "_" underscore and letters :
if you don't have let's gather data :
a-z alphabet list :
alphabet_list=list(map(chr,range(97,123)))
it will give :
>>> print(alphabet_list)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
next if you don't have underscore list :
underscore_list=[]
for i in range(1,27):
b.append("_")
and let's modify this list a little so make it like yours :
modified_underscore_list=['k', 'j', '_', '_', '_', '_', 'g', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']
Second step:
Now you have all things which needed to solve problem let's work on the second step now :
we have to map each element of first list to other list so we can save this result to dictionary format:
(Remember one thing dict keys can't be same or duplicate but values can be)
So let's iterate over list one and list two and then save the output to dictionary format :
For this purpose we need to iterate on both list so we will use built-in zip function :
final_output={} #we will save our iteration output in this dict
for i,j in zip(alphabet_list,modified_underscore_list):
final_output[i]=j
we can see the dict now :
{'p': '_', 'k': '_', 'w': '_', 't': '_', 'i': '_', 'c': '_', 'b': 'j', 'j': '_', 'a': 'k', 's': '_', 'g': 'g', 'x': '_', 'm': '_', 'l': '_', 'h': '_', 'o': '_', 'd': '_', 'n': '_', 'y': '_', 'r': '_', 'e': '_', 'u': '_', 'f': '_', 'v': '_', 'q': '_', 'z': '_'}
Now we have mapped data :
move to third and last step :
Third Step :
Now ask for input from user and check if character's of user's string in our final dictionary, if yes then replace only "a" "g" and "b" with value of those keys from our dictionary , simple:
ask_input=str(input("enter string"))
ask=list(ask_input)
for i,j in enumerate(ask):
if j in final_output:
if j=="a" or j=="b" or j=="g":
ask[i]=final_output.get(j)
print("".join(ask))
So our full code would be :
alphabet_list=list(map(chr,range(97,123)))
modified_underscore_list=['k', 'j', '_', '_', '_', '_', 'g', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']
final_output={}
for i,j in zip(alphabet_list,modified_underscore_list):
final_output[i]=j
ask_input=str(input("enter string"))
ask=list(ask_input)
for i,j in enumerate(ask):
if j in final_output:
if j=="a" or j=="b" or j=="g":
ask[i]=final_output.get(j)
print("".join(ask))
Upvotes: 0
Reputation: 46533
You could use str.translate
:
In [8]: from string import ascii_lowercase
In [9]: text.translate({ord(l): l if g == '_' else g for g, l in zip(guess, ascii_lowercase)})
Out[9]: 'i km jen .'
This maps elements of string.ascii_lowercase
to elements of guess
(by position). If an element of guess
is the underscore, the corresponding letter from ascii_lowercase
is used instead.
Upvotes: 2
Reputation:
Zip them:
import string
zipped = zip(string.ascii_lowercase, "kj____r___________________")
print(zipped)
# [('a', 'k'), ('b', 'j'), ('c', '_'), ('d', '_'), ('e', '_'), ('f', '_'), ('g', 'r'), ('h', '_'), ('i', '_'), ('j', '_'), ('k', '_'), ('l', '_'), ('m', '_'), ('n', '_'), ('o', '_'), ('p', '_'), ('q', '_'), ('r', '_'), ('s', '_'), ('t', '_'), ('u', '_'), ('v', '_'), ('w', '_'), ('x', '_'), ('y', '_'), ('z', '_')]
Convert it into a dict:
dict_ = dict(zipped)
print(dict_)
# {'a': 'k', 'b': 'j', 'c': '_', 'd': '_', 'e': '_', 'f': '_', 'g': 'r', 'h': '_', 'i': '_', 'j': '_', 'k': '_', 'l': '_', 'm': '_', 'n': '_', 'o': '_', 'p': '_', 'q': '_', 'r': '_', 's': '_', 't': '_', 'u': '_', 'v': '_', 'w': '_', 'x': '_', 'y': '_', 'z': '_'}
And then use a for loop for the substitution:
inp = "I am Ben."
result = ""
for letter in inp:
if letter in dict_:
if dict_[letter] != "_"
result += dict_[letter]
continue
result += letter
It all combined:
def sub(text, criteria):
import string
dict_ = dict(zip(string.ascii_lowercase, criteria))
result = ""
for letter in text:
if letter in dict_:
if dict_[letter] != "_":
result += dict_[letter]
continue
result += letter
return result
>>> sub("I am Ben. abg abg", "kj____r___________________")
'I km Ben. kjr kjr'
Upvotes: 0
Reputation: 16043
>>> text = "i am ben ."
>>> guess = "kj____r___________________"
>>> d = dict()
>>> for i in xrange(len(guess)):
... if(guess[i] != "_"):
... d[chr(i+97)] = guess[i]
...
>>> d
{'a': 'k', 'b': 'j', 'g': 'r'}
>>> text_list = list(text)
>>> text_list
['i', ' ', 'a', 'm', ' ', 'b', 'e', 'n', ' ', '.']
>>> for i in xrange(len(text_list)):
... if(text_list[i] in d):
... text_list[i] = d.get(text_list[i])
...
>>> text_list
['i', ' ', 'k', 'm', ' ', 'j', 'e', 'n', ' ', '.']
>>> text_final = "".join(text_list)
>>> text_final
'i km jen .'
>>>
Upvotes: 0
Reputation: 538
If you had a list of the alphabet, then the list of underscores, enter a for loop and then just compare the two values, appending to a list if it does or doesn’t
Upvotes: 0