繁体   English   中英

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:如果找到,则要打印之前和之后的元素,或它所跨越的元素/包含它的元素。

例如 ,对于第二个数组的[1,10],我想打印firstArray的[1,10],但是对于第二个数组的[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)

我也做了一些有关索引的研究,但无法弄清楚如何解决这个问题。 我还考虑过使用<=,> =,<和>,但是它随后将打印所有元素,其数量比在第一位置的搜索要少,但是它会比我想要的打印得多。

我可以使用映射和另一个数组的值从数组的长度“ ...”来“索引”,但这似乎是实现我想要的东西的一种相当复杂的方法。

提前致谢 :)

尝试这个:

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 ,然后在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]]

如果有人希望将其最小化,请执行!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM