我正在思考一种好的方法来查找两个列表中的差异。
下面是问题:
两个列表有一些字符串,其中前三个数字/字符(用*分隔)表示唯一键(后跟文本字符串="key1 * key2 * key3 * text")。
以下是字符串示例:
AA1*1D*4*The quick brown fox*****CC*3456321234543~
其中"*AA1*1D*4*"是一个唯一的键。
List1:"index1*index2*index3"、"index2*index2*index3"、"index3*index2*index3"
List2:"index2*index2*index3"、"index1*index2*index3"、"index3*index2*index3"、"index4*index2*index3"
我需要匹配两个列表中的索引并进行比较。
如果第一个列表中的全部三个索引与另一个列表中的三个索引匹配,则需要在新列表中跟踪这两个字符串条目。
如果一个列表中有一组索引在另一个列表中不存在,则需要跟踪一侧并在另一侧保留一个空条目。(例如上面的#4)
返回列表。
这是我到目前为止做的事情,但我有点困扰:
List<String> Base = baseListCopy.Except(resultListCopy, StringComparer.InvariantCultureIgnoreCase).ToList(); //Keep unique values(keep differences in lists)
List<String> Result = resultListCopy.Except(baseListCopy, StringComparer.InvariantCultureIgnoreCase).ToList(); //Keep unique values (keep differences in lists)
List<String[]> blocksComparison = new List<String[]>(); //we container for non-matching blocks; so we could output them later
//if both reports have same amount of blocks
if ((Result.Count > 0 || Base.Count > 0) && (Result.Count == Base.Count))
{
foreach (String S in Result)
{
String[] sArr = S.Split('*');
foreach (String B in Base)
{
String[] bArr = B.Split('*');
if (sArr[0].Equals(bArr[0]) && sArr[1].Equals(bArr[1]) && sArr[2].Equals(bArr[2]) && sArr[3].Equals(bArr[3]))
{
String[] NA = new String[2]; //keep results
NA[0] = B; //[0] for base
NA[1] = S; //[1] for result
blocksComparison.Add(NA);
break;
}
}
}
}
你能推荐一个适合这个过程的好算法吗?
谢谢