简体   繁体   中英

Fastest Way To Round Number Inside List of Strings Python

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.

You could do a little 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

Demo

Here is my solution. Fairly simple to understand - it loops through each item in the list, handles it on a string by string basis. It finds all numbers larger than 3 digits or more in that particular string, then replaces it with a rounded version. It finally prints everything.

import re
l = ['F/O 1751 HOBART PL NW', '11TH ST NW 2301', '801 MT VERNON ST NW']
for i in l:
    a = re.findall(r'(\d{3,})', i)
    s = re.sub( str(a[0]), str(round(int(a[0]),-2)), i)
    print(s)

This outputs the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM