【发布时间】:2022-12-02 22:40:30
【问题描述】:
我是 python 新手,想知道是否有解决此问题的最佳方法。 我有一个字符串模板,我想将其与字符串列表进行比较,如果发现任何差异,则从中创建一个字典。
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]},
]
我尝试了许多不同的方法,但最终陷入了一些随机逻辑,而且这些解决方案看起来不够通用,无法支持带有模板的任何字符串。 非常感谢任何帮助。
【问题讨论】:
标签: python-3.x regex