Python 3.x: 打印特定的数组项

Python 3.x: Print specific array items

我有两个嵌套数组,它们看起来像这样:

firstArray=[[1,10],[11,31],[32,40],[41,61],[62,78]]
secondArray=[[1,10],[12,32],[33,39],[41,78]]

我现在想在firstArray 中搜索secondArray 的元素。 我想要两个事件被区分:

1: 如果直接找到元素,我想打印出来。

2:如果没有找到,我想打印前后元素,或者[=51] =] 包含它的元素。

例如,对于second Array的[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)

我也做了一些关于索引的研究,但不知道如何解决这个问题。我也考虑过使用 <=、>=、< 和 >,但它会打印所有元素,其数量小于第一个位置的搜索,但它会打印比我想要的多得多。

我可以 "index" 使用一个映射和另一个数组,其值从 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 行。

如果您需要更多帮助,我会编辑我的答案并告诉您您所要求的解决方案

你可以这样试试:

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])

你可以试试这个,我正在打印值和它对应的结果。

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]

我有以下内容:

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]]

如果有人想将其最小化,请这样做!