【问题标题】:Create dictionary using string template and list of strings with differences使用字符串模板和有差异的字符串列表创建字典
【发布时间】: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


    【解决方案1】:

    您可以使用正则表达式

    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)
    

    它将产生以下输出

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

    它应该产生预期的输出,请检查是否有小错误......

    【讨论】:

    • 这个答案有帮助。我不知道如何在 Python 中使用正则表达式和 group() 方法。非常感谢
    【解决方案2】:

    我想使用命名的正则表达式组是解决这个问题的更优雅的方法,例如:

    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)
    

    输出:

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

    【讨论】:

      猜你喜欢
      • 2016-12-02
      • 2010-10-18
      • 1970-01-01
      • 2011-07-06
      • 2016-03-23
      • 2015-11-22
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      相关资源
      最近更新 更多