[英]C# Combine Two Lists With Overlapping Data
如果我有2個字符串列表
List<string> history = new List<string>(){ "AA", "BB", "CC", "AA" };
List<string> potentialNew = new List<string>(){ "CC", "AA", "DD", "EE", "FF", "AA"};
我需要一種方法來組合列表,同時防止“重疊”並保持相同的順序。 因此,在上面的示例中,將有一個組合列表:
AA, BB, CC, AA, DD, EE, FF, AA
換句話說,只有DD,EE,FF和AA被添加到history
列表中。
我一直試圖解決這個問題幾天,無數的搜索都沒有解決問題。 任何幫助將不勝感激!
如您在問題中提到的,這將為您提供給定輸入集的預期輸出:
List<string> history = new List<string>() { "AA", "BB", "CC", "AA" };
List<string> potentialNew = new List<string>() { "CC", "AA", "DD", "EE", "FF" };
var result = history.Concat(potentialNew.Where(x => !history.Contains(x)).ToList());
.Concat()
方法允許您連接兩個列表。 我們從potentialNew
中提取第一個List中不存在的特定項目,並將它們與第一個列表連接起來。
更新 :根據我們的討論,我得出的結論是,您正在尋找以下內容:
string lastItem = history.Last();
int lastIndexToCheck=history.Count-2,i=0;
for (; i < potentialNew.Count - 1; i++)
{
if (potentialNew[i] == lastItem && potentialNew[i - 1] == history[lastIndexToCheck])
{
break;
}
}
history.AddRange(potentialNew.Skip(i+1).ToList());
現在歷史將包含所需的元素集。
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
List<string> history = new List<string>(){ "AA", "BB", "CC", "AA" };
List<string> potentialNew = new List<string>(){ "CC", "AA", "DD", "EE", "FF" };
// make lists equal length
foreach(var x in history.ConcatOverlap(potentialNew)){
Console.WriteLine(x);
}
}
}
public static class Ext{
public static IEnumerable<string> ConcatOverlap(this List<string> history, List<string> potentialNew){
var hleng = history.Count();
var pleng = potentialNew.Count();
if(pleng > hleng) history = history.Concat(Enumerable.Range(1, pleng - hleng).Select(x => string.Empty)).ToList();
if(hleng > pleng) potentialNew = Enumerable.Range(1, hleng - pleng).Select(x => string.Empty).Concat(potentialNew).ToList();
var zipped = history.Zip(potentialNew, (a,b)=> new {First=a,Next=b, Equal = (a.Equals(b) || string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b))});
var count = 0;
var max = pleng > hleng ? pleng : hleng;
Console.WriteLine("Max " + max);
while(zipped.Any(x => !x.Equal) && count < max - 1){
count++;
potentialNew.Insert(0,string.Empty);
history.Add(string.Empty);
zipped = history.Zip(potentialNew, (a,b)=> new {First=a,Next=b, Equal = (a.Equals(b) || string.IsNullOrEmpty(a) || string.IsNullOrEmpty(b))});
}
return zipped.Select(x => string.IsNullOrEmpty(x.First) ? x.Next : x.First);
}
}
經過多一點考慮后:
public static IEnumerable<T> ConcatOverlap<T>(this IEnumerable<T> head, IEnumerable<T> tail){
var skip = 0;
var hLen = head.Count();
while(head.Skip(skip).Zip(tail, (a,b) => a.Equals(b)).Any(x => !x) && skip < hLen){
skip++;
}
return head.Take(skip).Concat(tail);
}
var history = new List<string>() { "AA", "BB", "CC", "AA" };
var potentialNew = new List<string>() { "CC", "AA", "DD", "EE", "FF" };
// Get the min. number of items to compare that 2 lists
for (int count = Math.Min(history.Count(), potentialNew.Count()); count >= 0; count--)
{
// Get the items from the back of history list, and get the items from potential list
// Compare them by SequenceEqual()
if (history.Skip(history.Count() - count).Take(count).SequenceEqual(potentialNew.Take(count)))
{
// Add the items to the result if found. It must be the greatest common part
return history.Concat(potentialNew.Skip(count));
}
}
這似乎很容易:
List<string> history = new List<string>(){ "AA", "BB", "CC", "AA" };
List<string> potentialNew = new List<string>(){ "CC", "AA", "DD", "EE", "FF", "AA"};
potentialNew.Aggregate(history, (h, p) =>
{
if (!h.Skip(h.Count - 2).Contains(p))
{
h.Add(p);
}
return h;
});
結果是history
包含:
AA BB CC AA DD EE FF AA
不確定這在性能方面有多好,但我建立了一個邏輯來實現你想要的。 任何人都可以通過tweek來讓它更清潔。
List<string> history = new List<string>() { "AA", "BB", "CC", "AA" };
List<string> potentialNew = new List<string>() { "CC", "AA", "DD", "EE", "FF", "AA" };
var result = ProcessChatLog(history,potentialNew);
//pass these two list to a function to process the chat log
核心邏輯就在這里。
public List<string> ProcessChatLog(List<string> history, List<string> potentialNew)
{
var lastChat = history.Last();
var lastChatIndex = history.Count - 1;
var allIndexWithLastChat = potentialNew.Select((c, i) => new { chat = c, Index = i })
.Where(x => x.chat == lastChat)
.Select(x => x.Index).Reverse().ToList();
List<int> IndexToClear = new List<int>();
bool overlapFound = false;
foreach (var index in allIndexWithLastChat)
{
if (!overlapFound)
{
int hitoryChatIndex = lastChatIndex;
IndexToClear.Clear();
for (int i = index; i > -1; i--)
{
if (potentialNew[i] == history[hitoryChatIndex])
{
IndexToClear.Add(i);
if (i == 0)
{
overlapFound = true;
break;
}
hitoryChatIndex--;
}
else
{
break;
}
}
}
else
{
IndexToClear.Clear();
break;
}
}
if(IndexToClear.Count >0)
{
potentialNew.RemoveRange(IndexToClear.Min(), IndexToClear.Count);
}
return history.Concat(potentialNew).ToList();
}
以下是一些結果
history = { "AA", "BB", "CC", "AA" }
potentialNew = { "CC", "AA", "DD", "EE", "FF", "AA"}
Result = { "AA", "BB","CC", "AA", "DD", "EE", "FF", "AA"}
history = { "AA", "BB","AA", "CC", "AA" }
potentialNew = { "AA","CC", "AA", "DD", "EE", "FF", "AA"}
Result = { "AA", "BB","AA","CC", "AA", "DD", "EE", "FF", "AA"}
history = { "AA", "BB", "CC", "AA" }
potentialNew = { "CC", "AA", "CC", "AA", "FF", "AA"}
Result = { "AA", "BB","CC", "AA", "CC", "AA", "FF", "AA"}
history = { "AA", "BB", "CC", "AA" }
potentialNew = {"AA", "CC", "AA", "DD", "EE", "FF", "AA" }
Result = { "AA", "BB","CC", "AA", "CC", "AA", "DD", "EE", "FF", "AA" }
如果這有幫助,請告訴我。
但我仍然說這不是你想要的好的輸出 。 因為如果聊天包含相同的消息20次,並假設你分別在11個和9個項目的2個列表中得到它。 現在根據您所需的輸出,您將省略所有9個消息新列表作為可能的重復和一個問題。 所以我說而不是這個解決方案,解決方案是跟蹤聊天記錄中傳遞的消息,並采取措施不在下一個日志中傳遞這些消息。這樣就可以保持邏輯和准確性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.