我有两个嵌套数组,它们看起来像这样:
firstArray=[[1,10],[11,31],[32,40],[41,61],[62,78]]
secondArray=[[1,10],[12,32],[33,39],[41,78]]
我现在想在firstArray中搜索secondArray的元素。 我希望区分两个事件:
1:如果直接找到该元素,我想打印它。
2:如果找不到 ,我想打印前面和后面的元素,或者它跨越的元素/包含它的元素。
例如,对于第二个数组[1,10]我想打印firstArray的[1,10],但是对于secondArray的[12,32]我想要打印firstArrays的[11,31] [32,40]。对于secondArray的[33,39]我想打印firstArray的[32,40]等等。
我知道,我可以使用嵌套for循环访问这两个数组,并且我可以通过索引访问这些元素。如果没有直接命中,我很难做到这一点。
对于直接点击,我正在执行以下操作:
foundYou=[]
for entry in firstArray:
for element in secondArray:
if(entry[0] == element[0]) and (entry[1] == element[1]):
foundYou.append(element)
我也做了一些关于索引的研究,但无法弄清楚,如何解决这个问题。我还想过使用< =,> =,<和>,但它会打印所有元素的数量小于第一个位置的搜索数量,但它会比我想要的打印得多。
我可以使用地图和另一个数组“索引”,其数值来自1 ...数组长度,但这似乎是实现我想要的相当复杂的方式。
提前致谢:)
答案 0 :(得分:1)
试试这个:
firstArray=[[1,10],[11,31],[32,40],[41,61],[62,78]]
secondArray=[[1,10],[12,32],[33,39],[41,78]]
foundYou=[]
didNotFindYou=[]
for element in secondArray:
if element in firstArray:
foundYou.append(element)
else:
index = firstArray[secondArray.index(element)]
nextindex = firstArray[secondArray.index(element)+1]
didNotFindYou.append([index, nextindex])
print('found:', foundYou)
print('did not find:', didNotFindYou)
输出:
found: [[1, 10]]
did not find: [[[11, 31], [32, 40]], [[32, 40], [41, 61]], [[41, 61], [62, 78]]]
我只通过secondArray
进行迭代,因为您说要检查secondArray
中的项是否在firstArray
中,这是第6行。然后我检查元素是否是在firstArray
中,这是第7行。然后我得到secondArray
中元素的索引,然后在firstArray
中获取具有相同索引的元素,这是在第10行然后我做了与第10行中提到的相同的事情,但只是我在索引中加1,这是第11行。
如果您需要更多帮助,我会编辑我的答案并告诉您所要求的解决方案
答案 1 :(得分:1)
您可以尝试这样:
firstArray=[[1,10],[11,31],[32,40],[41,61],[62,78]]
secondArray=[[1,10],[12,32],[33,39],[41,78]]
for index2 in range(len(secondArray)):
if secondArray[index2] == firstArray[index2]:
print(secondArray[index2])
else:
try:
print(firstArray[index2], firstArray[index2+1])
except IndexError as e:
print(e)
输出:
[1, 10]
[11, 31] [32, 40]
[32, 40] [41, 61]
[41, 61] [62, 78]
说明:
这里我们需要使用firstArray检查secondArray的元素。所以迭代第二个数组
for index2 in range(len(secondArray)):
使用if
检查第二个数组中的元素是否等于相应位置的第一个数组
if secondArray[index2] == firstArray[index2]:
如果条件满足打印值,否则必须打印第一个数组
中的元素和下一个元素print(firstArray[index2], firstArray[index2+1])
答案 2 :(得分:1)
您可以尝试这一点,我正在打印该值及其相应的结果。
firstArray=[[1,10],[11,31],[32,40],[41,61],[62,78]]
secondArray=[[1,10],[12,32],[33,39],[41,78]]
foundYou=[]
for second in secondArray:
for firstindex,first in enumerate(firstArray):
if second == first:
foundYou.append(first)
print(second,":",first)
else:
if second[0] >= first[0] and second[1] <= first[1]:
foundYou.append(first)
print(second,":",first)
else:
try:
if second[0] >= first[0] and second[1] <= firstArray[firstindex+1][1] and second[0] < first[1]:
foundYou.append(first)
foundYou.append(firstArray[firstindex+1])
print(second,":",first,firstArray[firstindex+1])
except IndexError:
pass
输出:
[1, 10] : [1, 10]
[12, 32] : [11, 31] [32, 40]
[33, 39] : [32, 40]
[41, 78] : [41, 61] [62, 78]
答案 3 :(得分:1)
我有以下内容:
firstArray=[[1,10],[11,31],[32,40],[41,61],[62,78]]
secondArray=[[1,10],[12,32],[33,39],[41,78]]
for s2, e2 in secondArray:
foundYou = []
for entry in firstArray:
s1, e1 = entry
if s1 <= s2 and e1 >= e2:
foundYou.append(entry) # We are fully contained within one entry
elif s1 <= s2 and e1 <= e2 and s2 <= e1:
foundYou.append(entry) # The start is within this entry but the end is in another
elif s1 >= s2 and e1 >= e2 and s1 <= e2:
foundYou.append(entry) # The end is within this entry but the start is in another
elif s1 >= s2 and e1 <= e2:
foundYou.append(entry) # This entry is entirely enveloped
print(foundYou)
输出:
[[1, 10]]
[[11, 31], [32, 40]]
[[32, 40]]
[[41, 61], [62, 78]]
如果有人想最大限度地减少这种情况,请做!