在python中只从列表中提取相关信息

2024-10-01 22:42:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含字符串子列表的列表。在

喜欢

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个值中的每一个都有自己的关键字。在

使用关键字从子列表中提取有用字符串的方法是什么?放弃其余的字符串?在上面的示例中,我只想保留“价格”、“权重”。在


Tags: 字符串ininfo列表关键字extrapricepower
3条回答

可以使用in关键字查看一个字符串(或列表)是否包含另一个字符串。 您可以使用any关键字一次检查多个项目。在

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']
]

keywords = ['Price', 'Weight']

for item in info:
    print([x for x in item if any(kw in x for kw in keywords)])

输出:

^{pr2}$

此数据的更干净的格式可能是使用字典。在

info = [
    {
        'Price': 5000, 
        'Weight': '8 kg',
        'Attributes': ['In warranty'] 
    },
    {
        'Price': 2800, 
        'Weight': '5.5 kg',
        'Attributes': ['Refundable', 'Extra battery power'] 
    },
    {
        'Price': 9000, 
        'Weight': '8 kg',
        'Attributes': ['Non-exchangeable', 'High-Quality'] 
    }
]

keywords = ['Price', 'Weight']

info_filterd = [{k: v for k, v in item.items() if k in keywords} for item in info]
print(info_filterd)

输出:

[
    {
        "Price": 5000,
        "Weight": "8 kg"
    },
    {
        "Price": 2800,
        "Weight": "5.5 kg"
    },
    {
        "Price": 9000,
        "Weight": "8 kg"
    }
]

使用函数编程的一个线性函数(map、filter和any)

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']
]

keywords = ['Price', 'Weight']

l = map(lambda sub_list: list(filter(lambda element: any(map(lambda keyword: keyword in element, keywords)), sub_list)), info)

print(list(l))

输出:

^{pr2}$

一层衬里各部分的说明

map(lambda sub_list: list(filter(lambda element: any(map(lambda keyword: keyword in element, keywords)), sub_list)), info)

迭代应用lambda函数的所有信息元素

filter(lambda element: any(map(lambda keyword: keyword in element, keywords)), sub_list)

在sub_list的所有值中,获取至少包含一个关键字的值(filter)

any(map(lambda keyword: keyword in element, keywords))

如果关键字中的任何关键字出现在元素中,则返回true或false

注意:list()用于展开生成器

使用difflib.SequenceMatcherdoc)的一个可能的解决方案。但是,可能需要对比率进行一些调整:

from difflib import SequenceMatcher

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']]

keywords = ['Price', 'Weight']

out = []
for i in info:
    out.append([])
    for item in i:
        if any(SequenceMatcher(None, item.lower(), kw.lower()).ratio() > 0.5 for kw in keywords):
            out[-1].append(item)

from pprint import pprint
pprint(out)

印刷品:

^{pr2}$

相关问题 更多 >

    热门问题