简体   繁体   中英

Extracting data according to a list

I'm trying to figure out how to extract some data from a string according to this list:

check_list = ['E1', 'E2', 'E7', 'E3', 'E9', 'E10', 'E12', 'IN1', 'IN2', 'IN4', 'IN10']

For example for this list:

s1 = "apto E1-E10 tower 1-2 sanit"

I would get ['E1', 'E10']

s2 = "apto IN2-IN1-IN4-E12-IN10 mamp"

For this I would get: ['IN2', 'IN1', 'IN4', 'E12', 'IN10']

And then this gets tricky:

s3 = "E-2-7-3-9-12; IN1-4-10 T 1-2 inst. hidr."

I would get: ['E2', 'E7', 'E3', 'E9', 'E12', 'IN1', 'IN4', 'IN10']

Can you please give some advice to solve this?

The following should work:

def extract_data(s):
    check_set = set(['E1', 'E2', 'E7', 'E3', 'E9', 'E10', 'E12',
                     'IN1', 'IN2', 'IN4', 'IN10'])
    result = []
    for match in re.finditer(r'\b(E|IN)[-\d]+', s):
        for digits in re.findall(r'\d+', match.group(0)):
            item = match.group(1) + digits
            if item in check_set:
                result.append(item)
    return result

Examples:

>>> extract_data("apto E1-E10 tower 1-2 sanit")
['E1', 'E10']
>>> extract_data("apto IN2-IN1-IN4-E12-IN10 mamp")
['IN2', 'IN1', 'IN4', 'E12', 'IN10']
>>> extract_data("E-2-7-3-9-12; IN1-4-10 T 1-2 inst. hidr.")
['E2', 'E7', 'E3', 'E9', 'E12', 'IN1', 'IN4', 'IN10']
import re

def parse(string):
  result = []
  for match in re.findall('(E|IN)-{0,1}([\d]+)((-[\d]+)*)', string):
    letter = match[0]
    numbers = [int(i) for i in [match[1]] + match[2].split('-')[1:]]
    for number in numbers:
      result.append('%s%d' % (letter, number))

  return result


print parse('apto E1-E10 tower 1-2 sanit')
print parse('apto IN2-IN1-IN4-E12-IN10 mamp')
print parse('E-2-7-3-9-12; IN1-4-10 T 1-2 inst. hidr.')

This is a partial answer, more of an indication how I might start to solve your issue.

Using the "keys" IN and E , I'd search the strings for patterns matching the key followed by any number of spaces or dashes.

For example:

import re

S = ['apto E1-E10 tower 1-2 sanit','apto IN2-IN1-IN4-E12-IN10 mamp','E-2-7-3-9-12; IN1-4-10 T 1-2 inst. hidr.']

for s in S:
    print s
    M = re.findall(r'(IN[\d\-]*)', s)
    for m in M: print m

    M = re.findall(r'(E[\d\-]*)', s)
    for m in M: print m

Produces:

$ python extract.py
apto E1-E10 tower 1-2 sanit
E1-
E10
apto IN2-IN1-IN4-E12-IN10 mamp
IN2-
IN1-
IN4-
IN10
E12-
E-2-7-3-9-12; IN1-4-10 T 1-2 inst. hidr.
IN1-4-10
E-2-7-3-9-12

I'd then take each m and parse it further. So that E1- resulted in [E1] and E-2-7-3-9-12 resulted in [E2,E7,E3,E9,E12] .

I tried to make this as general as possible:

import re

def make_relist(l):
    relist = []
    for a in l:
        alpha, num = re.match('([a-zA-Z]+)(\d+)', a).groups()
        re_string = r'\b{0}({1}|\d*-(\d+-)*{1})\b'.format(alpha, num)
        relist.append((a, re.compile(re_string)))

    return relist

def extract(s, relist):
   return [v for v, r in relist if r.search(s)]

Test:

>>> tokens = ['E1', 'E2', 'E7', 'E3', 'E9', 'E10', 'E12', 'IN1', 'IN2', 'IN4', 'IN10']
>>> relist = make_relist(tokens)
>>> extract("apto E1-E10 tower 1-2 sanit", relist)
['E1', 'E10']
>>> extract("apto IN2-IN1-IN4-E12-IN10 mamp", relist)
['E12', 'IN1', 'IN2', 'IN4', 'IN10']
>>> extract("E-2-7-3-9-12; IN1-4-10 T 1-2 inst. hidr.", relist)
['E2', 'E7', 'E3', 'E9', 'E12', 'IN1', 'IN4', 'IN10']

Note that this becomes more efficient if you have a large number of strings to extract from, because the compilation overhead time becomes insignificant in that case.

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