【问题标题】:Python 3.x: Print specific array itemsPython 3.x:打印特定的数组项
【发布时间】:2018-11-01 00:30:00
【问题描述】:

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

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...长度的数组,但这似乎是实现我想要的一种相当复杂的方法。

提前致谢:)

【问题讨论】:

  • 目前正在研究以下想法:将元素的当前位置 (=index) 存储在一个变量中,以通过访问 firstArray[variable.-1] 使用它来打印前后元素(例如)跨度>

标签: python arrays python-3.x for-loop try-except


【解决方案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]

【讨论】:

    【解决方案2】:

    你可以这样试试:

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

    【讨论】:

    • @Shushiro:已经过测试。让我知道这是你所期待的吗?
    • 是的,我想就是这样!由于我也收到了一些不合适的答案,我对问题的表述是否不好?我怎样才能更好地表达未来?
    • 其实,非常接近。例如,在第三行中,我不想要输出 [41,61]。搜索到的对 [33,39] 完全被 [32,40] 覆盖。但这是一个小问题,你已经帮了我很多了
    【解决方案3】:

    我有以下几点:

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

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

    【讨论】:

      【解决方案4】:

      试试这个:

      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 行的。

      如果您需要更多帮助,我将编辑我的答案并告诉您解决方案,以满足您的要求

      【讨论】:

      • 目前正在尝试,但收到 valueError,需要检查我的代码
      • @Shushiro 这对我来说很好用,顺便问一下你的错误信息是什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-03
      • 1970-01-01
      • 2018-07-11
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      相关资源
      最近更新 更多