如何比较两个列表元素?

问题描述 投票:0回答:5

我有两个清单

<cfset thelist1 = valueList(Gettest.full_name) /> 
<cfset thelist2 =ReplaceNoCase(colorList2,".jpg","","all")>

thelist1 =(test1,test2,test3,test4,test5)
thelist2 = (test1,test3)

如何比较 thelist1 和 thelist2 并从 thelist1 中获取不在 thelist2 中的元素?

我想也许要获取不在 thelist2 上的列表,我必须创建另一个 thelist3。

coldfusion coldfusion-9
5个回答
11
投票

我会使用一些java方法来做到这一点。这就是我所做的:

<cfset theArray1 = listToArray(thelist1)>
<cfset theArray2= listToArray(thelist2)>

现在如果我想保留匹配的项目,那么我会这样做:

<cfset theArray1.retainAll(theArray2) />

如果我想删除匹配的项目,那么像这样:

<cfset thearray1.removeAll(theArrar2) />

最后我会将数组转换为列表(如果需要)。

注意

removeAll
retainAll
是 java 方法,但在 ColdFusion 中可以正常工作,甚至不需要导入 java 库或包。


3
投票

另一种选择是使用闭包和标准列表函数来生成第一个列表中包含的所有元素的列表(或数组),但不是第二个:

resultArray = [];
listEach(firstList, function(value, index) {
    if (!listFindNoCase(secondList, value)) {
        arrayAppend(resultArray, value);
    }
});

话虽如此,看起来第一个列表的来源是数据库查询。如果第二个项目列表相对较小,也可以在数据库查询中完成此操作。只需使用

WHERE NOT IN (...)
子句即可检索所提供列表中 包含的所有值。像这样的东西:

QueryExecute("SELECT full_name FROM yourTable WHERE full_name NOT IN ( :filterList )"
    , { filterList={ value=secondList, cfsqltype="CF_SQL_VARCHAR", list="true"} }
);

2
投票

NewList 变量将包含 List2 中不存在的 List1

元素
<cfset thelist1 = "test1,test2,test3,test4,test5">
<cfset thelist2 = "test1,test3">

<cfset NewList = ListRemoveDuplicates(thelist1)>

<cfloop list="#thelist2#" index="i">
    <cfif listFindNoCase(NewList, i)>
         <cfset NewList = listdeleteat(NewList,listFindNoCase(NewList, i))>
    </cfif>
</cfloop>

<cfdump var="#NewList#" />

0
投票

在另一个循环内运行嵌套循环。

<cfscript>
    theList1 = listToArray("1,2,3");
    theList2 = listToArray("2,3");
    notInList = "";
    for ( i=1 ; i<=arrayLen(theList1) ; i++ ) {
        itemFound = false;
        for ( ii=1 ; ii<=arrayLen(theList2) ; ii++ ) {
            if( theList1[i] EQ theList2[ii] ) {
                itemFound = true;
                break;
            }
        }
        if( !itemFound ){
            notInList = listAppend(notInList,theList1[i]);
        }
    }
</cfscript>

您会在List1中找到List2中没有的项目


0
投票

函数 fLvL 返回数组

[onlyInFirst, onlyInSecond, inBoth]
:

<cffunction name="fLvL" hint="function List vs List: returns array[listOnlyInFirst, listOnlyInSecond, listInBoth]">
    <cfargument name="argL1" default="">
    <cfargument name="argL2" default="">
    <cfargument name="argDelim" default=",">
    <cfargument name="argDedup" default="1" hint="boolean 0/1 for Y/N, also will work w true/false">
    <cfargument name="argSort" default="0" hint="boolean 0/1 for Y/N, also will work w true/false">
    <cfargument name="argSortType" default="textNoCase" hint="others: numeric, text (case sens), aabzABZ, aAaBbBzzZ">

    <cfset var raRet=["","",""]>
    <cfif !len(argL1)>
        <CFRETURN ["", argL2, ""]>
    <cfelseif !len(argL2)>
        <CFRETURN [argL1, "", ""]>
    </cfif>
    <cfloop index="iL1item" list="#argL1#">
        <cfif listFindNoCase(argL2, iL1item, argDelim)>
            <cfset raRet[3] = listAppend(raRet[3], iL1item, argDelim)>
        <cfelse>
            <cfset raRet[1] = listAppend(raRet[1], iL1item, argDelim)>
        </cfif>
    </cfloop>
    <cfloop index="iL2item" list="#argL2#">
        <cfif !listFindNoCase(argL1, iL2item, argDelim)>
            <cfset raRet[2] = listAppend(raRet[2], iL2item, argDelim)>
        </cfif>
    </cfloop>
    <cfif argSort>
        <cfloop index="iCnt" from="1" to="3">
            <cfset reRet[iCnt] = listSort(reRet[iCnt], argSortType, argDelim)>
        </cfloop>
    </cfif>
    <cfif argDedup>
        <cfloop index="iCnt" from="1" to="3">
            <cfset reRet[iCnt] = listRemoveDuplicates(reRet[iCnt], argDelim)>
        </cfloop>
    </cfif>
    <cfreturn raRet>
</cffunction>

<cfset raResult=fLvL("first,list","second,list")>
<cfdump var="#raResult#">
<!--- returns: ["first","second","list"] --->

raResult=fLvL("first,list","second,list")
返回数组:
["first","second","list"]

还提供了一些进一步的论证选项(可能有点矫枉过正)。

© www.soinside.com 2019 - 2024. All rights reserved.