Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.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

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
2个python列表中的公共列表_Python_List - Fatal编程技术网

2个python列表中的公共列表

2个python列表中的公共列表,python,list,Python,List,我在python中有两个列表,如下所示: a = [['1490526293.84', '2010113827', 'amazon.com', '208.67.220.220'], ['1490526293.78', '2010113827', 'amazon.com', '208.67.222.222'], ['1490525901.72', '2010113827', 'amazon.com', '8.8.4.4'], ['1490526293.66', '20

我在python中有两个列表,如下所示:

a = [['1490526293.84', '2010113827', 'amazon.com', '208.67.220.220'],
     ['1490526293.78', '2010113827', 'amazon.com', '208.67.222.222'],
     ['1490525901.72', '2010113827', 'amazon.com', '8.8.4.4'],
     ['1490526293.66', '2010113827', 'amazon.com', '8.8.8.8'],
     ['1490525291.24', '151249113', 'google.com', '208.67.220.220'],
     ['1490524291.18', '151249113', 'google.com', '208.67.222.222'],
     ['1490526289.02', '151249113', 'google.com', '8.8.4.4'],
     ['1490526288.96', '151249113', 'google.com', '8.8.8.8'],
     ['1490525291.46', '2017032579', 'intuit.com', '208.67.220.220'],
     ['1490526291.41', '2017032579', 'intuit.com', '208.67.222.222'],
     ['1490526291.35', '2017032579', 'intuit.com', '8.8.4.4'],
     ['1490526291.29', '2017032579', 'intuit.com', '8.8.8.8']]

b = [['1490526293.66', '2010113827', 'amazon.com', '8.8.8.8'],
     ['1490526288.96', '151249113', 'google.com', '8.8.8.8'],
     ['1490526291.29', '2017032579', 'intuit.com', '8.8.8.8']]
for item in a:
    if item not in b:
        print item
[['1490525901.72', '2010113827', 'amazon.com', '8.8.4.4'],
 ['1490525291.24', '151249113', 'google.com', '208.67.220.220'],
 ['1490524291.18', '151249113', 'google.com', '208.67.222.222'],
 ['1490525291.46', '2017032579', 'intuit.com', '208.67.220.220']]
for item_a in a:
    if item_a not in b:
        for item_b in b:
            if item_a[2] == item_b[2]:
                if float(item_b[0]) - float(item_a[0]) > 300:
                    print item_a
对于列表中的每个项目:

  • 第0项为历元时间
  • 第1项是序列号
  • 第2项是域名
  • 第3项是DNS_服务器
最初的要求是在a中查找b中不存在的每个列表。这很简单,具体操作如下:

a = [['1490526293.84', '2010113827', 'amazon.com', '208.67.220.220'],
     ['1490526293.78', '2010113827', 'amazon.com', '208.67.222.222'],
     ['1490525901.72', '2010113827', 'amazon.com', '8.8.4.4'],
     ['1490526293.66', '2010113827', 'amazon.com', '8.8.8.8'],
     ['1490525291.24', '151249113', 'google.com', '208.67.220.220'],
     ['1490524291.18', '151249113', 'google.com', '208.67.222.222'],
     ['1490526289.02', '151249113', 'google.com', '8.8.4.4'],
     ['1490526288.96', '151249113', 'google.com', '8.8.8.8'],
     ['1490525291.46', '2017032579', 'intuit.com', '208.67.220.220'],
     ['1490526291.41', '2017032579', 'intuit.com', '208.67.222.222'],
     ['1490526291.35', '2017032579', 'intuit.com', '8.8.4.4'],
     ['1490526291.29', '2017032579', 'intuit.com', '8.8.8.8']]

b = [['1490526293.66', '2010113827', 'amazon.com', '8.8.8.8'],
     ['1490526288.96', '151249113', 'google.com', '8.8.8.8'],
     ['1490526291.29', '2017032579', 'intuit.com', '8.8.8.8']]
for item in a:
    if item not in b:
        print item
[['1490525901.72', '2010113827', 'amazon.com', '8.8.4.4'],
 ['1490525291.24', '151249113', 'google.com', '208.67.220.220'],
 ['1490524291.18', '151249113', 'google.com', '208.67.222.222'],
 ['1490525291.46', '2017032579', 'intuit.com', '208.67.220.220']]
for item_a in a:
    if item_a not in b:
        for item_b in b:
            if item_a[2] == item_b[2]:
                if float(item_b[0]) - float(item_a[0]) > 300:
                    print item_a
但是,现在我需要在a中查找未出现在b中的列表,并且对于b中的列表,特定域的历元时差也超过5分钟,因此输出如下:

a = [['1490526293.84', '2010113827', 'amazon.com', '208.67.220.220'],
     ['1490526293.78', '2010113827', 'amazon.com', '208.67.222.222'],
     ['1490525901.72', '2010113827', 'amazon.com', '8.8.4.4'],
     ['1490526293.66', '2010113827', 'amazon.com', '8.8.8.8'],
     ['1490525291.24', '151249113', 'google.com', '208.67.220.220'],
     ['1490524291.18', '151249113', 'google.com', '208.67.222.222'],
     ['1490526289.02', '151249113', 'google.com', '8.8.4.4'],
     ['1490526288.96', '151249113', 'google.com', '8.8.8.8'],
     ['1490525291.46', '2017032579', 'intuit.com', '208.67.220.220'],
     ['1490526291.41', '2017032579', 'intuit.com', '208.67.222.222'],
     ['1490526291.35', '2017032579', 'intuit.com', '8.8.4.4'],
     ['1490526291.29', '2017032579', 'intuit.com', '8.8.8.8']]

b = [['1490526293.66', '2010113827', 'amazon.com', '8.8.8.8'],
     ['1490526288.96', '151249113', 'google.com', '8.8.8.8'],
     ['1490526291.29', '2017032579', 'intuit.com', '8.8.8.8']]
for item in a:
    if item not in b:
        print item
[['1490525901.72', '2010113827', 'amazon.com', '8.8.4.4'],
 ['1490525291.24', '151249113', 'google.com', '208.67.220.220'],
 ['1490524291.18', '151249113', 'google.com', '208.67.222.222'],
 ['1490525291.46', '2017032579', 'intuit.com', '208.67.220.220']]
for item_a in a:
    if item_a not in b:
        for item_b in b:
            if item_a[2] == item_b[2]:
                if float(item_b[0]) - float(item_a[0]) > 300:
                    print item_a
到目前为止,我所做的尝试如下:

a = [['1490526293.84', '2010113827', 'amazon.com', '208.67.220.220'],
     ['1490526293.78', '2010113827', 'amazon.com', '208.67.222.222'],
     ['1490525901.72', '2010113827', 'amazon.com', '8.8.4.4'],
     ['1490526293.66', '2010113827', 'amazon.com', '8.8.8.8'],
     ['1490525291.24', '151249113', 'google.com', '208.67.220.220'],
     ['1490524291.18', '151249113', 'google.com', '208.67.222.222'],
     ['1490526289.02', '151249113', 'google.com', '8.8.4.4'],
     ['1490526288.96', '151249113', 'google.com', '8.8.8.8'],
     ['1490525291.46', '2017032579', 'intuit.com', '208.67.220.220'],
     ['1490526291.41', '2017032579', 'intuit.com', '208.67.222.222'],
     ['1490526291.35', '2017032579', 'intuit.com', '8.8.4.4'],
     ['1490526291.29', '2017032579', 'intuit.com', '8.8.8.8']]

b = [['1490526293.66', '2010113827', 'amazon.com', '8.8.8.8'],
     ['1490526288.96', '151249113', 'google.com', '8.8.8.8'],
     ['1490526291.29', '2017032579', 'intuit.com', '8.8.8.8']]
for item in a:
    if item not in b:
        print item
[['1490525901.72', '2010113827', 'amazon.com', '8.8.4.4'],
 ['1490525291.24', '151249113', 'google.com', '208.67.220.220'],
 ['1490524291.18', '151249113', 'google.com', '208.67.222.222'],
 ['1490525291.46', '2017032579', 'intuit.com', '208.67.220.220']]
for item_a in a:
    if item_a not in b:
        for item_b in b:
            if item_a[2] == item_b[2]:
                if float(item_b[0]) - float(item_a[0]) > 300:
                    print item_a

这是疯狂的嵌套,我正在寻找更好的替代方案

我有办法解决您的问题,但时间复杂度可能很高

for item_a in a:
  if item_a not in b:
     for item_b in b:
         if item_a[2]==item_b[2]:
             if abs(float(item_a[0])-float(item_b[0]))>5*60:
                 print item_a
                 break

如果列表a(项目a)和项目表单列表b(项目b)中的项目具有相同的域名,并且项目a[0]和项目b[0]之间的差值的绝对值大于5*60,则打印项目a。

出于某种原因,我不能仅发表评论,因此我将在这里写下它

因为您的子列表是唯一的,也许您可以尝试使用这些集合运算符之一


这个问题对我来说不够清楚。你能删除所有与问题无关的信息吗?@Xcoder先生..编辑过..如果更清楚请告诉我..还有..为什么否决..这太广泛了..我给出了确切的问题陈述..给出了我尝试过的代码..并要求一个非常具体的答案..我不是否决者,但是我猜原因很可能是因为你的问题是关于如何编写一段你没有尝试过的代码。在你的问题中,你展示了你是如何解决问题的“a”部分的。但是“b”部分是一个完全不同的问题,您没有显示任何代码。你试过解决这部分问题吗?如果是这样,那么提供代码并解释您遇到的困难。到目前为止您尝试了什么?您可以像以前一样使用嵌套的if语句,为a中的每个列表检查它是否出现在b中;如果没有,则检查历元时间是否在b中出现的任何历元时间的5分钟内(这可以通过循环所有b来实现)。@bestbzw..这与我的尝试非常相似..我正在寻找嵌套较少的解决方案。。