Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 提取列表中与字符串匹配的所有元素_Python_Python 3.x_List - Fatal编程技术网

Python 提取列表中与字符串匹配的所有元素

Python 提取列表中与字符串匹配的所有元素,python,python-3.x,list,Python,Python 3.x,List,我有一个关键字列表和一个列表输入列表。我的任务是找到那些包含关键字(甚至部分)的列表。我可以使用以下代码提取包含关键字的列表: t_列表=['小计:','1292.80'],['增值税','64.64'],['收据总额','AED1357.44'], [“未选定,高达2000”,“小计”,“60.58”], [“未选择,高达500”,“金额160.58”,“3.03”], ['Learn'、'Bectricity total'、''、'63.61'] 关键字=['total','amount'

我有一个关键字列表和一个列表输入列表。我的任务是找到那些包含关键字(甚至部分)的列表。我可以使用以下代码提取包含关键字的列表:


t_列表=['小计:','1292.80'],['增值税','64.64'],['收据总额','AED1357.44'],
[“未选定,高达2000”,“小计”,“60.58”],
[“未选择,高达500”,“金额160.58”,“3.03”],
['Learn'、'Bectricity total'、''、'63.61']
关键字=['total','amount']
对于t_列表中的列表:
对于表中的字符串列表:
字符串列表[:]=[字符串列表中项目的项目,如果项目!='']
对于字符串列表中的元素:
element=element.lower()
如果有关键字中的元素中的,则为:
打印(字符串列表)
输出为:
[['小计:','1292.80'],['收据总额','1357.44'],['未选定,高达2000','小计','60.58'],['未选定,高达500','金额160.58','3.03'],
['Learn'、'Bectricity total'、'63.61']]
所需的输出只有与关键字和列表中的数字匹配的字符串

所需输出:

['小计:','1292.80'],['收据总额','1357.44'],['小计','60.58'],['金额160.58','3.03'],['现金总额','63.61']]
如果我可以将输出作为一个字典,将与关键字匹配的字符串作为键,将数字作为值,那就太完美了


提前谢谢你

以下是我们聊天中的答案,稍作修改,添加了一些注释,作为对代码的一些解释。请随时要求我澄清或更改任何内容

import re

t_list = [
    ['Subtotal: ', '1,292.80 '],
    ['VAT ', ' 64.64 '],
    ['RECEIPT TOTAL ', 'AED1,357.44 '],
    ['NOT_SELECTED, upto2,000 ', 'Sub total ', '60.58 '],
    ['NOT_SELECTED, upto500 ', 'amount 160.58 ', '', '3.03 '],
    ['Learn', 'Bectricity total ', '', '', '63.61 ']
]

keywords = ['total ', 'amount ']

output = {}

for sub_list in t_list:
    # Becomes the string that matched the keyword if one is found
    matched = None

    for item in sub_list:
        for keyword in keywords:
            if keyword in item.lower():
                matched = item

    # If a match was found, then we start looking at the list again
    # looking for the numbers
    if matched:
        for item in sub_list:
            # split the string so for example 'amount 160.58 ' becomes ['amount', '160.58']
            # This allows us to more easily extract just the number
            split_items = item.split()
            for split_item in split_items:
                # Simple use of regex to match any '.' with digits either side
                re_search = re.search(r'[0-9][.][0-9]', split_item)
                if re_search:
                    # Try block because we are making a list. If the list exists, 
                    # then just append a value, otherwise create the list with the item
                    # in it
                    try:
                        output[matched.strip()].append(split_item)
                    except KeyError:
                        output[matched.strip()] = [split_item]

print(output)

您提到要匹配字符串,例如
'AED 63.61'
。我的解决方案是使用
.split()。例如,对于像
'amount 160.58'
这样的字符串,只需抓取
160.58
就变得容易多了。我不知道如何匹配一个字符串,比如你想要保留的字符串,但不匹配我刚才提到的字符串(当然,除非它只是
'AED'
,在这种情况下,我们可以添加一些逻辑来匹配任何与
'AED'
)的字符串。

我对这个问题有点困惑。这些清单中有很多似乎都是随机的。因此,您正在检查该列表中是否有任何元素与关键字匹配,但您想要哪个数字?只有与关键字匹配的同一字符串中出现的数字?或者列表中的任何数字?例如,在“NOT_SELECTED,upto500”列表中,有2个数字(如果算500,则为3),您想要哪个数字。好的,我将尝试解释这些步骤:好的,我将尝试解释这些步骤:步骤1:在
t_list
中搜索关键字,并返回与关键字和列表中存在的数字匹配的元素。从
['NOT_SELECTED,upto500','amount 160.58','3.03']
,我想要
['amount 160.58','3.03']
。第一个元素是因为关键字amount和数字“3.03”,因为它是一个数字@RolvapResethso每个列表中的最后一项是否总是您想要的号码?因为例如,
“AED1357.44”如何计数并不总是这样。如果我能找到匹配关键字的元素和所有有数字的元素。我可以和那个@RolvApneseth一起工作