1

I am new to python and wanted to know if there are best approaches for solving this problem. I have a string template which I want to compare with a list of strings and if any difference found, create a dictionary out of it.

template = "Hi {name}, how are you? Are you living in {location} currently? Can you confirm if following data is correct - {list_of_data}"
list_of_strings = [
    "Hi John, how are you? Are you living in California currently? Can you confirm if following data is correct - 123, 456, 345",
    "Hi Steve, how are you? Are you living in New York currently? Can you confirm if following data is correct - 6542"
]
expected = [
    {"name": "John", "location": "California", "list_of_data": [123, 456, 345]},
    {"name": "Steve", "location": "New York", "list_of_data": [6542]},
]

I tried many different approaches but ended up stuck in some random logics and the solutions did not look generic enough to support any string with the template. Any help is highly appreciated.

2 Answers 2

2

You can use regular-expression

template = "Hi {name}, how are you? Are you living in {location} currently? Can you confirm if following data is correct - {list_of_data}"
list_of_strings = [
    "Hi John, how are you? Are you living in California currently? Can you confirm if following data is correct - 123, 456, 345",
    "Hi Steve, how are you? Are you living in New York currently? Can you confirm if following data is correct - 6542"
]

import re
expected = []
for s in list_of_strings:
    r_ = re.search("Hi (.+)?, how are you\? Are you living in (.+?) currently\? Can you confirm if following data is correct - (.+)", s)
    res = {}
    res["name"] = r_.group(1)
    res["location"] = r_.group(2)
    res["list_of_data"] = list(map(int, (r_.group(3).split(","))))
    expected.append(res)
print(expected)

It will produce following output

[{'name': 'John', 'location': 'California', 'list_of_data': [123, 456, 345]}, {'name': 'Steve', 'location': 'New York', 'list_of_data': [6542]}]

It should produce expected output, please check for minor bugs if any ...

Sign up to request clarification or add additional context in comments.

1 Comment

This answer helped. I was not aware about how to use regular expressions in Python and about group() method. Thanks a lot
0

I guess using named regular expression groups is more elegant way to solve this problem, for example:

import re 

list_of_strings = [
    "Hi John, how are you? Are you living in California currently? Can you confirm if following data is correct - 123, 456, 345",
    "Hi Steve, how are you? Are you living in New York currently? Can you confirm if following data is correct - 6542"
]

pattern = re.compile(
    r"Hi (?P<name>(.+)?), how are you\? "
    r"Are you living in (?P<location>(.+)) currently\? "
    r"Can you confirm if following data is correct - (?P<list_of_data>(.+))"
)

result = []
for string in list_of_strings:
    if match := pattern.match(string):
        obj = match.groupdict()
        obj['list_of_data'] = list(map(int, obj['list_of_data'].split(',')))
        result.append(obj)

print(result)

Output:

[
    {'name': 'John', 'location': 'California', 'list_of_data': [123, 456, 345]},
    {'name': 'Steve', 'location': 'New York', 'list_of_data': [6542]}
]

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.