-1

Trying to split my address string with

apts = {'apt','apartment','unit','spc','space','trlr','lot','A','B','C','D'}

could it be done any better way than

fulladdress.split("apt")
fulladdress.split("apartment")
...
...

was trying to make

def split_all(text, dict):
for i in dict():
    text = text.split(i)
return text

s = split_all(s,apts)

"fulladdress = "213 house rd apt 1011" I want to get "213 house rd 1011"

didn't work out. I feel like missing something

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
Minnu P
  • 63
  • 1
  • 9

3 Answers3

1

You can use list comprehension to split it all at once. It will return a list where each item is a kind of split using different keywords in apts.

splits = [fulladdress.split(apt) for apt in apts]
meelo
  • 572
  • 4
  • 14
  • As `apts` is a set, the order of results in `splits` could not be determined which might not make it that easy to use. – Martin Evans Jul 08 '15 at 17:25
0

You can use re compiling the pattern using word boundaries, you cannot split as you will split on substrings:

s = "213 house rd apt 1011"
apts = ['apartment','unit','space',"spc","apt",'trlr','lot','A','B','C','D']
import re
r = re.compile(r"\b|\b".join(apts))

print(r.sub("", s))
213 house  rd  1011
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

It is difficult to understand what you are trying to achieve. I am guessing you have quite a lot of different source addresses and are trying to extract the first line from each of them. Ideally it would be easier if we could see some more examples to give you a more precise filter.

I am guessing apts lists possible delimter points that you have determined for where the end of the first line is. If this is the case then the following would be a straight forward solution for you to follow:

fulladdress = "213 house rd apt 1011"
apts = ['apt','apartment','unit','spc','space','trlr','lot','A','B','C','D','house']
first_part = ""

for search in apts:
    index = fulladdress.find(" %s " % search)

    if index != -1:
        first_part = fulladdress[:index]
        break

print first_part

It simply tries to find one of the matching search parameters and returns the address up to that point. The code returns:

213 house rd
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
  • thanks a lot! I was wondering if it's multiple rows, how can I "skip" the ones that does't matches with apts? I mean, when the address has no match with the list of words. – Minnu P Jul 08 '15 at 19:15
  • I'm pooling it out of DB which just look like 1324 addres apt; if address contains word in the list, then get rid of the rest if not just keep it as is. – Minnu P Jul 08 '15 at 19:35
  • Think I got my answer myself. Thanks again – Minnu P Jul 08 '15 at 19:41