Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.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/9/csharp-4.0/2.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_Nested Lists - Fatal编程技术网

Python 在任意数量的嵌套列表中查找范围内的数字

Python 在任意数量的嵌套列表中查找范围内的数字,python,nested-lists,Python,Nested Lists,我有任意数量的嵌套列表(为了简单起见,假设有两个),它们的长度都是一样的: 编辑 在本次编辑中,我将示例列表更改为两个特定的列表,这似乎会带来麻烦: l1 = [[96, 110], [49, 95, 122], [173, 218], [30], [80, 159], [95, 119, 150, 168]] l2 = [[25, 110], [63, 126], [130, 222], [42], [3], [94, 119, 150, 176]] 现在,我想要一个函数

我有任意数量的嵌套列表(为了简单起见,假设有两个),它们的长度都是一样的:

编辑 在本次编辑中,我将示例列表更改为两个特定的列表,这似乎会带来麻烦:

l1 = [[96, 110], [49, 95, 122], [173, 218], [30], [80, 159], [95, 119, 150, 168]]
l2 = [[25, 110], [63, 126],     [130, 222], [42], [3],       [94, 119, 150, 176]]
现在,我想要一个函数,它检查每个索引,在给定范围内的每个列表中是否存在条目(以及条目的数量和数量),并返回它们
假设范围是20。在这个例子中,我想返回

[[[110, 96], [110, 110]], [[63, 49], [126, 122]], [222, 218], [42, 30], [], [[95,94],[119, 119], [150, 150], [176, 168]]]
我知道对于两个列表,我可以使用
itertools
如下:

result = []
for i in range(len(l1): # the lists have the same length 
  result.append(
  [[a,b] for (a, b) in itertools.product(l1[i], l2[i]) 
                 if a-20 <= b <=a+20])
或顺序l2,l1

[[[110, 110]], [], [], [[30, 42]], [], [[95, 94], [119, 119], [150, 150], [168, 150]]]

乐于接受任何建议

一旦您解决了两个列表的问题,您可以从前两个列表开始迭代使用它,然后合并列表1和列表2,并在合并列表和列表3之间执行检查,然后将列表3合并到该列表,并用列表4处理合并列表,依此类推

通过对列表1中的子列表进行排序,并使用对分_left查找>=到a-20的第一个元素“b”,然后在排序的元素中按顺序进行,直到超出a+20,可以大大加快两个列表之间的比较逻辑。您可以对列表2中相应子列表中的每个项目“a”执行此操作。这将使您的时间复杂度为O(NlogM),而不是O(N*M),当您在多列表过程中合并列表时,时间复杂度将变得更加重要

下面是多列表过程的一个具体示例

请注意,我没有在MatchSublist函数中包含对分搜索优化(仅当子列表足够大时才需要)

输出:

l1 = [[80,112,270], [20,78],  [6],             [99,134,240,300]]
l2 = [[30],         [22,84],  [7,122,189,279], [67,100]]
l3 = [[60],         [25, 70], [2],             [110]]

result = matchMultiLists(l1,l2,l3, match=lambda a,b:abs(a-b)<=20)
print(result)

[
  [(80, 60)],
  [(20, 22), (78, 84), (20, 25), (22, 25), (78, 70), (84, 70)],
  [(6, 7), (6, 2), (7, 2)],
  [(99, 100), (99, 110), (100, 110)]
]
由于可以将MatchMultiList与两个列表一起使用,因此不需要将排序添加到Match2List()函数中。事实上,3个单行函数可以在matchMultiList()函数中定义,以避免暴露它们

输出:

l1=[[96, 110], [49, 95, 122], [173, 218], [30], [80, 159], [95, 119, 150, 168]]
l2=[[25, 110], [63, 126],     [130, 222], [42], [3],       [94, 119, 150, 176]]

range20 = lambda a,b:abs(a-b)<=20

print(matchMultiLists(l1,l2, match=range20))
[[(96, 110), (110, 110)], [(49, 63), (122, 126)], [(218, 222)], [(30, 42)], [], [(94, 95), (119, 119), (150, 150), (150, 168), (168, 176)]]

print(matchMultiLists(l2,l1, match=range20))
[[(96, 110), (110, 110)], [(49, 63), (122, 126)], [(218, 222)], [(30, 42)], [], [(94, 95), (119, 119), (150, 150), (150, 168), (168, 176)]]
l1=[[96110]、[4995122]、[173218]、[30]、[80159]、[95119150168]]
l2=[[25110]、[63126]、[130222]、[42]、[3]、[94119150176]]

range20=lambda a,b:abs(a-b)一旦您解决了两个列表的问题,您可以通过从前两个列表开始迭代使用它,然后合并列表1和列表2,并在合并列表和列表3之间执行检查,然后将列表3合并到该列表,并使用列表4处理合并列表,依此类推

通过对列表1中的子列表进行排序,并使用对分_left查找>=到a-20的第一个元素“b”,然后在排序的元素中按顺序进行,直到超出a+20,可以大大加快两个列表之间的比较逻辑。您可以对列表2中相应子列表中的每个项目“a”执行此操作。这将使您的时间复杂度为O(NlogM),而不是O(N*M),当您在多列表过程中合并列表时,时间复杂度将变得更加重要

下面是多列表过程的一个具体示例

请注意,我没有在MatchSublist函数中包含对分搜索优化(仅当子列表足够大时才需要)

输出:

l1 = [[80,112,270], [20,78],  [6],             [99,134,240,300]]
l2 = [[30],         [22,84],  [7,122,189,279], [67,100]]
l3 = [[60],         [25, 70], [2],             [110]]

result = matchMultiLists(l1,l2,l3, match=lambda a,b:abs(a-b)<=20)
print(result)

[
  [(80, 60)],
  [(20, 22), (78, 84), (20, 25), (22, 25), (78, 70), (84, 70)],
  [(6, 7), (6, 2), (7, 2)],
  [(99, 100), (99, 110), (100, 110)]
]
由于可以将MatchMultiList与两个列表一起使用,因此不需要将排序添加到Match2List()函数中。事实上,3个单行函数可以在matchMultiList()函数中定义,以避免暴露它们

输出:

l1=[[96, 110], [49, 95, 122], [173, 218], [30], [80, 159], [95, 119, 150, 168]]
l2=[[25, 110], [63, 126],     [130, 222], [42], [3],       [94, 119, 150, 176]]

range20 = lambda a,b:abs(a-b)<=20

print(matchMultiLists(l1,l2, match=range20))
[[(96, 110), (110, 110)], [(49, 63), (122, 126)], [(218, 222)], [(30, 42)], [], [(94, 95), (119, 119), (150, 150), (150, 168), (168, 176)]]

print(matchMultiLists(l2,l1, match=range20))
[[(96, 110), (110, 110)], [(49, 63), (122, 126)], [(218, 222)], [(30, 42)], [], [(94, 95), (119, 119), (150, 150), (150, 168), (168, 176)]]
l1=[[96110]、[4995122]、[173218]、[30]、[80159]、[95119150168]]
l2=[[25110]、[63126]、[130222]、[42]、[3]、[94119150176]]

range20=lambda a,b:abs(a-b)这是我根据您提到的内容制作的:

l1 = [[80,112,270],[20,78], 6,             [99,134,240,300]]
l2 = [30,          [22,84],[7,122,189,279],[67,100]]
l3 = [60, [25, 70], [2], [110]]

def makeZip(maxRange, *args):
    for l in args: #For each index in the lists, converts any integers to lists
        for i in range(len(l)):
            if type(l[i]) == int:
                l[i] = [l[i]]

    z = zip(*args)
    #Zip makes lists for each video with all of the entries
    #Basically Equivilant to transposing a matrix in Lin Alg
    matches = []
    for l in z: #For each video, generates matching pairs
        videoMatches = []
        for m in makeMatch(maxRange, l): #For all of the pairs, add to list
            videoMatches.append(m)
        matches.append(videoMatches) #Add the list to the main list

    return matches

def makeMatch(maxRange, l):
    if len(l) == 1: #If only one list (person) then return all of the values sequentially (generator)
        for i in l[0]:
            yield [i]
        return

    matches = []
    for n in makeMatch(maxRange, l[1:]): #for each of the generated match from a recursive call
        for i in l[0]: #For all of the values of the current person
            if all([abs(i - x) < maxRange for x in n]): #Check if value works for all of the values already in the match
                matches.append([i] + n) #Sucessful match

    for m in matches: #when done with all of the matches, return them sequentially (generator)
        yield m

for m in makeZip(20, l1, l2, l3):
    print(m)
l1=[[80112270],[20,78],6[99134240300]
l2=[30[22,84],[7122189279],[67100]]
l3=[60、[25,70]、[2]、[110]]
def makeZip(最大范围,*args):
对于参数中的l:#对于列表中的每个索引,将所有整数转换为列表
对于范围内的i(len(l)):
如果类型(l[i])==int:
l[i]=[l[i]]
z=zip(*args)
#Zip为每个视频制作包含所有条目的列表
#基本上相当于在Lin Alg中转置矩阵
匹配项=[]
对于z中的l:#对于每个视频,生成匹配对
视频匹配=[]
对于makeMatch(maxRange,l)中的m:#对于所有对,添加到列表中
videoMatches.append(m)
匹配。附加(视频匹配)#将列表添加到主列表中
复赛
def makeMatch(最大范围,l):
如果len(l)==1:#如果只有一个列表(人员),则按顺序返回所有值(生成器)
对于l[0]中的i:
收益率[i]
返回
匹配项=[]
对于makeMatch中的n(maxRange,l[1:]):#对于从递归调用生成的每个匹配
对于l[0]中的i:#对于当前人员的所有值
if all([abs(i-x)
不过,您可能需要重命名这些变量。希望输出是三个列表的应有结果


这个解决方案可能存在的一个问题是,我很确定O(numVideos^numPeople)在最坏的情况下是匹配的。但是,关于复杂性可能是错误的。

这是我根据您提到的内容制作的:

l1 = [[80,112,270],[20,78], 6,             [99,134,240,300]]
l2 = [30,          [22,84],[7,122,189,279],[67,100]]
l3 = [60, [25, 70], [2], [110]]

def makeZip(maxRange, *args):
    for l in args: #For each index in the lists, converts any integers to lists
        for i in range(len(l)):
            if type(l[i]) == int:
                l[i] = [l[i]]

    z = zip(*args)
    #Zip makes lists for each video with all of the entries
    #Basically Equivilant to transposing a matrix in Lin Alg
    matches = []
    for l in z: #For each video, generates matching pairs
        videoMatches = []
        for m in makeMatch(maxRange, l): #For all of the pairs, add to list
            videoMatches.append(m)
        matches.append(videoMatches) #Add the list to the main list

    return matches

def makeMatch(maxRange, l):
    if len(l) == 1: #If only one list (person) then return all of the values sequentially (generator)
        for i in l[0]:
            yield [i]
        return

    matches = []
    for n in makeMatch(maxRange, l[1:]): #for each of the generated match from a recursive call
        for i in l[0]: #For all of the values of the current person
            if all([abs(i - x) < maxRange for x in n]): #Check if value works for all of the values already in the match
                matches.append([i] + n) #Sucessful match

    for m in matches: #when done with all of the matches, return them sequentially (generator)
        yield m

for m in makeZip(20, l1, l2, l3):
    print(m)
l1=[[80112270],[20,78],6[99134240300]
l2=[30[22,84],[7122189279],[67100]]
l3=[60、[25,70]、[2]、[110]]
def makeZip(最大范围,*args):
对于参数中的l:#对于列表中的每个索引,将所有整数转换为列表
对于范围内的i(len(l)):
如果类型(l[i])==int:
l[i]=[l[i]]
z=zip(*args)
#Zip为每个视频制作包含所有条目的列表
#基本上等同于转置m
import itertools

l1 = [[1, [4, 11], 2], [3,4]]
l2 = [[5,6], [7,8]]
l3 = [[9, 10], [11, 12]]

result = []

def flatten(l):
    for el in l:
        if isinstance(el, list):
            yield from flatten(el)
        else:
            yield el

for sub_lists in zip(l1, l2 ,l3):
    for couple_subs in itertools.combinations(sub_lists, 2):
        result.append(
                      [[a,b] for a, b in itertools.product(flatten(couple_subs[0]), flatten(couple_subs[1]))
                       if abs(a-b) <= 3])

print(result)
[[[4, 5], [4, 6], [2, 5]], [[11, 9], [11, 10]], [[6, 9]], [[4, 7]], [], [[8, 11]]]