我有一个包含字符串子列表的列表。在
喜欢:
info = [['Price: 5000', 'In warranty', 'Weight: 8 kg'],
['Refundable', 'Price: 2800', 'Weight: 5.5 kg', 'Extra battery power'],
['Price: 9000', 'Non-exchangeable', 'Weight: 8 kg', 'High-Quality']..]
每个子列表都有一些无关的额外字符串。我只需要在子列表中准确的5个值,最好地描述产品的信息,这5个值中的每一个都有自己的关键字。在
使用关键字从子列表中提取有用字符串的方法是什么?放弃其余的字符串?在上面的示例中,我只想保留“价格”、“权重”。在
可以使用
in
关键字查看一个字符串(或列表)是否包含另一个字符串。 您可以使用any
关键字一次检查多个项目。在输出:
^{pr2}$此数据的更干净的格式可能是使用字典。在
输出:
使用函数编程的一个线性函数(map、filter和any)
输出:
^{pr2}$一层衬里各部分的说明
迭代应用lambda函数的所有信息元素
在sub_list的所有值中,获取至少包含一个关键字的值(filter)
如果关键字中的任何关键字出现在元素中,则返回true或false
注意:list()用于展开生成器
使用
difflib.SequenceMatcher
(doc)的一个可能的解决方案。但是,可能需要对比率进行一些调整:印刷品:
^{pr2}$相关问题 更多 >
编程相关推荐