0

I have a address list as :

addr = ['100 NORTH MAIN ROAD',
            '100 BROAD ROAD APT.',
            'SAROJINI DEVI ROAD',
            'BROAD AVENUE ROAD']

I need to do my replacement work in a following function:

def subst(pattern, replace_str, string):

by defining a pattern outside of this function and passing it as an argument to subst.

I need an output like:

addr = ['100 NORTH MAIN RD',
            '100 BROAD RD APT.',
            'SAROJINI DEVI RD ',
            'BROAD AVENUE RD']

where all 'ROAD' strings are replaced with 'RD'


def subst(pattern, replace_str, string):
  #susbstitute pattern and return it
  new=[]
  for x in string:
    new.insert((re.sub(r'^(ROAD)','RD',x)),x)
  return new

def main():
  addr = ['100 NORTH MAIN ROAD',
        '100 BROAD ROAD APT.',
        'SAROJINI DEVI ROAD',
        'BROAD AVENUE ROAD']

  #Create pattern Implementation here 
  pattern=r'^(ROAD)'
  print (pattern)
  #Use subst function to replace 'ROAD' to 'RD.',Store as new_address
  new_address=subst(pattern,'RD',addr)
  return new_address

I have done this and getting below error

Traceback (most recent call last):

File "python", line 23, in File "python", line 20, in main File "python", line 7, in subst

TypeError: 'str' object cannot be interpreted as an integer

4
  • 1
    Please make an attempt and show what you have tried before asking for help Commented Feb 20, 2018 at 13:31
  • Thanks @sshashank124, I will attach it . Commented Feb 20, 2018 at 13:35
  • def subst(pattern, replace_str, string): #susbstitute pattern and return it new=[] for x in string: new.insert((re.sub(r'^(ROAD)','RD',x)),x) return new def main(): addr = ['100 NORTH MAIN ROAD', '100 BROAD ROAD APT.', 'SAROJINI DEVI ROAD', 'BROAD AVENUE ROAD'] #Create pattern Implementation here pattern=r'^(ROAD)' #Use subst function to replace 'ROAD' to 'RD.',Store as new_address new_address=subst(pattern,'RD',addr) return new_address print (main()) Commented Feb 20, 2018 at 13:36
  • Edit it into the question itself Commented Feb 20, 2018 at 13:37

5 Answers 5

2

No need for regex, just use replace:

[x.replace('ROAD', 'RD') for x in addr]

If you only want to replace the ROAD as a word, no in the middle, use:

[re.sub(r'\bROAD\b', 'RD', x) for x in addr]
Sign up to request clarification or add additional context in comments.

3 Comments

ya how can I pass this r'\bROAD\b' as a parameter(pattern) to that subst function?
@prasannakumar Pass it as it is.
Tysm :) I got the op
0

Using re

import re
addr = ['100 NORTH MAIN ROAD',
            '100 BROAD ROAD APT.',
            'SAROJINI DEVI ROAD',
            'BROAD AVENUE ROAD']

for i, v in enumerate(addr):
    addr[i] = re.sub('ROAD', 'RD', v) #v.replace("ROAD", "RD")

print addr

Output:

['100 NORTH MAIN RD', '100 BRD RD APT.', 'SAROJINI DEVI RD', 'BRD AVENUE RD']

Comments

0

Using RegEx..

import re
count = 0
for x in addr:
    addr[count] = re.sub('ROAD', 'RD', x)
    count = count + 1

addr    # just to print the result.

3 Comments

yes, instead of passing 'road' directly to re.sub() method, I need to pass it by using reg.expression .
@prasannakumar didn't get what you said. So you must extract the pattern "ROAD" from a string and, after, replace this by 'RD'? What I think you are trying to do is do iterate over addr and while dong this, replace 'ROAD' BY 'RD' using regex. If this is the case, I don't understand what aren't you satisfied with wiht answer :/
Thanks :) I got the answer, btw pattern=r'\bROAD\b' this is i needed
0

I got the answer,

pattern=r'\bROAD\b'

passing this a parameter to

def subst(pattern, replace_str, string):

    #susbstitute pattern and return it
    new=[re.sub(pattern,'RD',x) for x in string]
    return new

we can get the op :)

Comments

0

Try this:

#!/bin/python3

import sys
import os
import io
import re


# Complete the function below.
def subst(pattern, replace_str, string):
    #susbstitute pattern and return it
    new_address=[pattern.sub(replace_str,st) for st in string]
    return new_address


def main():
    addr = ['100 NORTH MAIN ROAD',
            '100 BROAD ROAD APT.',
            'SAROJINI DEVI ROAD',
            'BROAD AVENUE ROAD']
            
    #Create pattern Implementation here 
    pattern=re.compile(r'\bROAD')
    #Use subst function to replace 'ROAD' to 'RD.',Store as new_address
    new_address=subst(pattern,'RD.',addr)
    print(new_address)
    return new_address

'''For testing the code, no input is required'''

if __name__ == "__main__":
  main()
  
  
def bind(func):
    func.data = 9
    return func

@bind
def add(x, y):
    return x + y

print(add(3, 10))
print(add.data)

1 Comment

Please check the following ressource: meta.stackoverflow.com/editing-help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.