I want to remove all the digits from the string other than the digits given in the dictionary. I have written code to remove it but not getting the expected result. Please have a look below:
mystr=" hey I want to delete all the digits ex 600 502 700 m 8745 given in this string. And getting request from the ip address 122521587502. This string tells about deleting digits 502 600 765 from this."
myDict={'600','700'} # set()
# snippet to remove digits from string other than digits given in the myDict
My Solution
for w in myDict:
for x in mystr.split():
if (x.isdigit()):
if(x != w):
mystr.replace(x," ")
Expected result:
mystr=" hey I want to delete all the digits ex 600 700 m given in this string. And getting request from the
ip address . This string tells about deleting digits 600 from this."
inoperator. Also, never change an iterator that you're looping over.