python - 在字符串列表中查找子字符串列表的索引;填写缺失值
问题描述
我试图确保预期的子字符串列表出现在字符串列表中。我需要知道是否缺少一个,以便我可以填充它。我需要在字符串列表中找到子字符串列表的索引,以便可以提取旁边的字符串的值。(使用 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'}
解决方案
这个问题似乎是关于如何将解决问题所需的逻辑步骤转换为代码。甚至在开始使用 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'}]
推荐阅读
- laravel - Laravel:两个表单,一个提交按钮
- android-studio - 如何防止在我的对象上使用 kotlin 扩展?
- python - 使用另一个类的 __init__ 值
- tensorflow - Tensorflow 以与输入相同的顺序获取输入
- cloud-foundry - 有没有办法在不存储凭据的情况下从应用程序获取 UAA 访问令牌?
- php - 从数据库中获取用户喜欢的所有帖子
- java - 如何处理网络状态变化的在线媒体播放器(WiFi 或移动数据之间的网络转换)?
- hibernate - Hibernate - 脏检查不适用于@Convert
- scala - 这是什么意思 - 未应用的方法仅在需要函数类型时才转换为函数
- javascript - Angular 对象不支持 Internet Explorer 的属性或方法“scrollTo”