Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Python 2.7 - Fatal编程技术网

在Python中的列表列表中查找部分匹配

在Python中的列表列表中查找部分匹配,python,list,python-2.7,Python,List,Python 2.7,我有一个列表列表,我需要找到并打印包含完全匹配和部分匹配的列表,使用两个条件忽略大小写 l = [['2014','127.0.0.1','127','DNS sever Misconfiguration'],['2014','192.168.1.25','529','Failed logon for user user1'],['2014','127.0.0.1','1','This is a test message']] 条件1和2可以是任何情况,即“192.186.1.25”或“失败”

我有一个列表列表,我需要找到并打印包含完全匹配和部分匹配的列表,使用两个条件忽略大小写

l = [['2014','127.0.0.1','127','DNS sever Misconfiguration'],['2014','192.168.1.25','529','Failed logon for user user1'],['2014','127.0.0.1','1','This is a test message']]
条件1和2可以是任何情况,即“192.186.1.25”或“失败”

>>> for i in l:
        if 'condition 1' in i and 'condition2' in i:
           print i
给。。。没什么

我只能使用一个具有精确匹配的条件并得到一个结果

>>> for i in l:
      if '127.0.0.1' in i:
        print i


['2014', '127.0.0.1', '127', 'DNS sever Misconfiguration']
['2014', '127.0.0.1', '1', 'This is a test message']
任何帮助都将不胜感激

'condition 1' in i
将搜索字符串文字
“条件1”
。相反,我认为您的意思是搜索名称
条件1
引用的对象,即:

condition1 in l
如果需要“部分”匹配,请使用

if condition1 in l or condition2 in l:
any()


我的猜测是,你只是没有正确地匹配第二个条件,例如,如果你这样做:

'127.0.0.1' in i and 'Misconfiguration' in i
但是
i
看起来像:

['2014', '127.0.0.1', '127', 'DNS sever Misconfiguration']
然后,
'127.0.0.1'
将出现在
i
中,但
'Misconfiguration'
不会出现,因为它是一个列表,而
中的
对于列表是完全匹配的,但您要查找的是
i
元素的子字符串。如果这些一致,您可以执行以下操作:

'127.0.0.1' in i and 'Misconfiguration' in i[3]
或者,如果不是,则必须对子字符串检查所有条目:

'127.0.0.1' in i and any('Misconfiguration' in x for x in i)
我应该这样做。这将子字符串检查
i
中的每个项目以查找您的搜索词。

以下是我的尝试:

l = [['2014','127.0.0.1','127','DNS sever Misconfiguration'],    ['2014','192.168.1.25','529','Failed logon for user user1'],['2014','127.0.0.1','1','This is a test message']]

condition1 = 'Failed'
condition2 = '2014'

for lst in l:
    found_cond1 = False
    found_cond2 = False
    for string in lst:
        if condition1 in string:
            found_cond1 = True
        if condition2 in string:
            found_cond2 = True
        if found_cond1 and found_cond2:
            print(lst)
            break
给予


谢谢,any()条件找到了正确的列表,但仅用于精确匹配。是否有方法匹配任何部分,如'192'将拾取字符串192.168.1.25?Nest
any
如果有(any(any)(cond in li表示cond in(…))表示li in l):
感谢您的回复,i[3]中的'condition'起作用,但any()找不到第二个条件。第二个条件是什么?这是有效的:
i=['2014'、'127.0.0.1'、'127'、'DNS服务器错误配置']
'127.0.0.1'在i中以及任何('Misconfiguration'在x中表示x在i中)
->
True
l = [['2014','127.0.0.1','127','DNS sever Misconfiguration'],    ['2014','192.168.1.25','529','Failed logon for user user1'],['2014','127.0.0.1','1','This is a test message']]

condition1 = 'Failed'
condition2 = '2014'

for lst in l:
    found_cond1 = False
    found_cond2 = False
    for string in lst:
        if condition1 in string:
            found_cond1 = True
        if condition2 in string:
            found_cond2 = True
        if found_cond1 and found_cond2:
            print(lst)
            break
['2014', '192.168.1.25', '529', 'Failed logon for user user1']