Fastest Way To Round Number Inside List of Strings Python
Question: Given a list (can be numpy array) of addresses:

    
    
    >input: ['F/O 1751 HOBART PL NW', '11TH ST NW 2301', '801 MT VERNON ST NW']
    

where the number doesn't always occur at the same place in the string. Is
there a faster way than first extracting the number with regex and then
inserting it back somehow in order to round it to the nearest (lets say) 100.

    
    
    >output: ['F/O 1800 HOBART PL NW', '11TH ST NW 2300', '800 MT VERNON ST NW']
    

Any help would be appreciated. Thank you,

EDIT:

Only Numbers delimited by word boundaries (space, period, comma) would need to
be converted so r'\b\d+\b' would work.


Answer: You could do a little [text
munging](https://docs.python.org/2/library/re.html#text-munging) with
`re.sub()`:

    
    
    import re
    
    def replace_number(n):
        return str(int(round(int(n.group(0)), -2)))
    
    data = ['F/O 1751 HOBART PL NW', '11TH ST NW 2301', '801 MT VERNON ST NW']
    
    ## I'm assuming you only want to munge numbers with 3 or more digits:
    for datum in data:
        print re.sub(r"(\d{3,})", replace_number, datum)
    

output:

    
    
    F/O 1800 HOBART PL NW
    11TH ST NW 2300
    800 MT VERNON ST NW
    

**Note** \- this will give you potentially undesirable results if there is a
number like 020 in the string:

    
    
    '020 MT VERNON ST NW'
    

becomes

    
    
    '0 MT VERNON ST NW'
    

If you expect to find that in your data, you'll need to add some checks to the
`replace_number(n)` function

