1
$\begingroup$

I am working on the models of different product types and wish to generalize them to the same entity. For example, from the given list

Toshiba-A40C
Toshiba B30
Toshiba-Z40C411
Asus -X540
Asus R4
Dell XPS 15
Dell Inspiron 13

I would like to get a clean list of

Toshiba
Toshiba
Toshiba
Asus
Asus
Dell
Dell

Is there a programmatic way to get this one ? Is this called a record linkage? What are your recommendations ?

$\endgroup$
2
  • $\begingroup$ Index-Match or Vlookup in Excel could accomplish this simply. R base code allows you to look up a value in a dataset (like the one you have above) given an input value. $\endgroup$
    – ERT
    Commented Jul 24, 2018 at 20:39
  • $\begingroup$ Thank you @ERT . What if the first word is misspelled such as Thoshiba- A40C. Is there a way we can generalize it to 'Toshiba' ? $\endgroup$
    – user_01
    Commented Jul 24, 2018 at 20:43

1 Answer 1

3
$\begingroup$

I know that there are better solutions to this problem. I used a regular expression to match first space or dash and then cut the original list.

import re

example = """Toshiba-A40C
Toshiba B30
Toshiba-Z40C411
Asus -X540
Asus R4
Dell XPS 15
Dell Inspiron 13""".split('\n')

regular_exp = '(?: +)|(-)'

for i in range(len(example)):
    where = re.search(regular_exp, example[i]).span()
    example[i] = example[i][:where[0]]

print(example)
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.