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. Amu help is highly appreciated.
CodePudding user response:
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 ...
CodePudding user response:
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]}
]