簡體   English   中英

比較兩個列表中的元素

[英]Compare elements in two lists

我有兩個包含整數的列表,如下所示:

def first = [10, 12, 3, 6, 9, 8, 33]
def second = [8, 9, 5, 6]

每個列表中的元素數量可以完全任意。 我還有一個閾值,它也是一個數字:

def threshold = 3 

我必須成對檢查比較兩個數組中的所有元素,並檢查它們的差是否小於等於閾值。 結果,我必須輸出所有這些元素。

所以,在我的情況,它是: 10, 12, 3, 6, 9, 8從第一列表和8, 9, 5, 6從第二。 因為

abs(10-8) <= 3
abs(12-9) <= 3
abs(3-5) <= 3
abs(6-6) <= 3

由於第一個列表比第二個列表包含更多的元素,因此我必須將第一個列表元素與第二個列表中的最后一個元素進行比較。

abs(9-6) <= 3
abs(8-6) <= 3
abs(33-6) >= 3, stop here!

Groovy和Java答案是合適的。

PS這是一個算法問題,並且已經存在一些用於此目的的算法嗎?

這是完成它的Java方法。 由於您使用abs()進行減法,所以先出現是無關緊要的。 因此,讓我們利用它並找出更長的時間。 一旦知道了這一點,我們就知道要強制重復哪一個是最后一個元素。

if (first.length() > second.length()) {
    longer = first;
    shorter = second;
} else {
    longer = second;
    shorter = first;
}

last = shorter.length() - 1;

for(int i = 0; i < longer.length(); i++) {
    s = shorter.get(Math.min(last, i));
    l = longer.get(i);
    if (abs(l-s) > threshold) {
        break;
    }
}

在以下實現中,我使用了set數據結構來防止結果中的元素重復,如果要顯示重復的元素,則可以使用arraylist代替set

import java.util.*;
   public class CompareElements{



      public static void main(String[ ] arg){


        int [] firstList = {10, 12, 3, 6, 9, 8, 33};
        int [] secondList = {8, 9, 5, 6};

        int firstListLength = firstList.length;
        int secondListLength = secondList.length;
        // i have used set data structure to prevent duplication of elements in the result
        Set<Integer>result=new HashSet<Integer>();

        // iterate over the two list and get the absolute value for each two corresponding elements
        // and check if the difference is <= 3 , the two elements are added to the result 
        for(int i=0;i<Math.min(firstList.length, secondList.length);i++) {
            if(Math.abs(firstList[i]-secondList[i]) <= 3)
            {
                result.add(firstList[i]);
                result.add(secondList[i]);
            }
        }

        // here we are trying to handle the case when the lists have different lengths
        // and the second list length is greater 
        if(firstListLength < secondListLength)
        {
            for(int i =firstListLength-1;i<secondListLength;i++)
            {
                if(Math.abs(firstList[firstListLength-1]-secondList[i]) <= 3)
                {
                    result.add(firstList[firstListLength-1]);
                    result.add(secondList[i]);
                }
            }
        }
        // here we are trying to handle the case when the lists have different lengths
        // and the first list length is greater 
        else if (firstListLength > secondListLength)
        {
            for(int i =secondListLength-1;i<firstListLength;i++)
            {
                if(Math.abs(firstList[i]-secondList[secondListLength-1]) <= 3)
                {
                    result.add(firstList[i]);
                    result.add(secondList[secondListLength-1]);
                }
            }
        }
        System.out.println(result.toString());





       }

  }

如果您不需要可怕的最終重復元素限制,那么通常是:

[first,second].transpose()
              .every { Math.abs(it[0]-it[1]) < threshold }

編輯

鑒於此功能可以將列表填充到一定寬度(默認為列表的最大長度):

def paddedPairs(List lists, Integer width=lists*.size().max()) {
    (0..<width).collect { p -> lists.collect { it[ p >= it.size() ? -1 : p] } }
}

你可以做:

paddedPairs([first, second]).every { Math.abs(it[0]-it[1]) < threshold }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM