首页 > 解决方案 > 在字符串列表中查找子字符串列表的索引;填写缺失值

问题描述

我试图确保预期的子字符串列表出现在字符串列表中。我需要知道是否缺少一个,以便我可以填充它。我需要在字符串列表中找到子字符串列表的索引,以便可以提取旁边的字符串的值。(使用 Python 3。)

# List of strings parsed from a document
strings = [['name', 'Joe Sixpack', 'email', 'beerme@thebrew.com'],
           ['name', 'Winnie Cooler', 'email', 'Winnie Cooler', 'phone', 
            '555-555-5550']]
# Expected/desired headings
subs = ['name', 'email', 'phone']

然后检查是否捕获了所有“潜艇”。如果没有,找到哪些并用 nan 填写。

预期成绩:

{'name': 'Joe Sixpack', 'email': 'beerme@thebrew.com', 'phone': nan}
{'name': 'Winnie Cooler', 'email': 'Winnie Cooler', 'phone': '555-555- 
 5550'}

标签: pythonsubstringlist-comprehension

解决方案


这个问题似乎是关于如何将解决问题所需的逻辑步骤转换为代码。甚至在开始使用 Python 之前,用伪代码思考以清楚地看到所需的逻辑步骤会很有帮助。

for each row of data:
    * initialize a new output data structure for this row
    for each required key:
        if the key is in the row:
            * find the indices associated with the key/value pair
            * store key/value pair in the output data
        otherwise (i.e. if the key is not in the row):
            * store key/None pair in the output data 

您几乎可以直接将此伪代码转换为工作 Python 代码。这是一种非常明确的方法,在逻辑的每个步骤中都使用循环和变量声明,这非常适合作为学习练习。稍后,您可能希望针对性能和/或样式对其进行优化。

# List of strings parsed from a document
strings = [['name', 'Joe Sixpack', 'email', 'beerme@thebrew.com'],
           ['name', 'Winnie Cooler', 'email', 'Winnie Cooler', 'phone', 
            '555-555-5550']]

# Expected/desired headings
subs = ['name', 'email', 'phone']

# Create dictionaries for each row
results = []  
for row in strings:
    d = {}
    for key in subs:
        if key in row:
            key_idx = row.index(key)
            val_idx = key_idx + 1
            val = row[val_idx]
        else:
            val = None
        d[key] = val
    results.append(d)

print(results)

结果:

[{'name': 'Joe Sixpack', 'email': 'beerme@thebrew.com', 'phone': None}, 
{'name': 'Winnie Cooler', 'email': 'Winnie Cooler', 'phone': '555-555-5550'}]

推荐阅读