比较两个列表并找到这两个列表之间的增量的最有效的模式/算法是什么?
我们有两个列表,比如说学生和他们的分数。我想比较这两个列表并找到新列表和旧列表之间的增量,然后找到侵入性最小的方式将任何更改插入或更新到新列表中。解决这个问题的最佳算法是什么?希望专注于对新列表和性能进行最少的更改。
示例代码:
List<ListItem> existingList = new List<ListItem>();
List<ListItem> newList = new List<ListItem>();
public TopLists()
{
InitTwoLists();
}
private void InitTwoLists()
{
existingList.Add(new ListItem { Name = "Shane", Score = 100 });
existingList.Add(new ListItem { Name = "Mark", Score = 95 });
existingList.Add(new ListItem { Name = "Shane", Score = 94 });
existingList.Add(new ListItem { Name = "Steve", Score = 90 });
existingList.Add(new ListItem { Name = "Brian", Score = 85 });
existingList.Add(new ListItem { Name = "Craig", Score = 85 });
existingList.Add(new ListItem { Name = "John", Score = 82 });
existingList.Add(new ListItem { Name = "Steve", Score = 81 });
existingList.Add(new ListItem { Name = "Philip", Score = 79 });
existingList.Add(new ListItem { Name = "Peter", Score = 70 });
newList.Add(new ListItem { Name = "Shane", Score = 100 });
newList.Add(new ListItem { Name = "Steve", Score = 96 }); // This is change
newList.Add(new ListItem { Name = "Mark", Score = 95 });
newList.Add(new ListItem { Name = "Shane", Score = 94 });
newList.Add(new ListItem { Name = "Brian", Score = 85 });
newList.Add(new ListItem { Name = "Craig", Score = 85 });
newList.Add(new ListItem { Name = "John", Score = 82 });
newList.Add(new ListItem { Name = "Steve", Score = 81 });
newList.Add(new ListItem { Name = "Philip", Score = 79 });
newList.Add(new ListItem { Name = "Peter", Score = 70 });
}
}
public void CompareLists()
{
// How would I find the deltas and update the new list with any changes from old?
}
}
public class ListItem
{
public string Name { get; set; }
public int Score { get; set; }
}
** 编辑:所需输出 ***
所需输出是实际更改带有增量的 newList。 例如,在这种情况下:
newList.Add(new ListItem { Name = "Shane", Score = 100 });
newList.Add(new ListItem { Name = "Steve", Score = 96 }); // This is change
newList.Add(new ListItem { Name = "Mark", Score = 95 });
newList.Add(new ListItem { Name = "Shane", Score = 94 });
newList.Add(new ListItem { Name = "Brian", Score = 85 });
newList.Add(new ListItem { Name = "Craig", Score = 85 });
newList.Add(new ListItem { Name = "John", Score = 82 });
newList.Add(new ListItem { Name = "Steve", Score = 81 });
newList.Add(new ListItem { Name = "Roger", Score = 80 }); // Roger is a new entry
newList.Add(new ListItem { Name = "Phillip", Score = 79 }); // Philip moved down one
// Peter 以其 70 分的成绩从该列表中删除,因为我只想要前 10 名。
因此更改将是:
更新“Steve”的记录 2,分数已更改 在位置 9 插入新记录“Roger” 打破了“Peter”跌出前十名的记录。
We have two lists, let's say students and their scores. I want to compare these two lists and find the delta between the new list and the old list, then find the least intrusive way to Insert or Update into the new list any changes. What is the best algorithm to approach this? Want to focus on minimal amount of changes to new list and performance.
Example code:
List<ListItem> existingList = new List<ListItem>();
List<ListItem> newList = new List<ListItem>();
public TopLists()
{
InitTwoLists();
}
private void InitTwoLists()
{
existingList.Add(new ListItem { Name = "Shane", Score = 100 });
existingList.Add(new ListItem { Name = "Mark", Score = 95 });
existingList.Add(new ListItem { Name = "Shane", Score = 94 });
existingList.Add(new ListItem { Name = "Steve", Score = 90 });
existingList.Add(new ListItem { Name = "Brian", Score = 85 });
existingList.Add(new ListItem { Name = "Craig", Score = 85 });
existingList.Add(new ListItem { Name = "John", Score = 82 });
existingList.Add(new ListItem { Name = "Steve", Score = 81 });
existingList.Add(new ListItem { Name = "Philip", Score = 79 });
existingList.Add(new ListItem { Name = "Peter", Score = 70 });
newList.Add(new ListItem { Name = "Shane", Score = 100 });
newList.Add(new ListItem { Name = "Steve", Score = 96 }); // This is change
newList.Add(new ListItem { Name = "Mark", Score = 95 });
newList.Add(new ListItem { Name = "Shane", Score = 94 });
newList.Add(new ListItem { Name = "Brian", Score = 85 });
newList.Add(new ListItem { Name = "Craig", Score = 85 });
newList.Add(new ListItem { Name = "John", Score = 82 });
newList.Add(new ListItem { Name = "Steve", Score = 81 });
newList.Add(new ListItem { Name = "Philip", Score = 79 });
newList.Add(new ListItem { Name = "Peter", Score = 70 });
}
}
public void CompareLists()
{
// How would I find the deltas and update the new list with any changes from old?
}
}
public class ListItem
{
public string Name { get; set; }
public int Score { get; set; }
}
** EDIT: Desired Output ***
The desired output is to actually change the newList with the deltas.
For example in this scenario:
newList.Add(new ListItem { Name = "Shane", Score = 100 });
newList.Add(new ListItem { Name = "Steve", Score = 96 }); // This is change
newList.Add(new ListItem { Name = "Mark", Score = 95 });
newList.Add(new ListItem { Name = "Shane", Score = 94 });
newList.Add(new ListItem { Name = "Brian", Score = 85 });
newList.Add(new ListItem { Name = "Craig", Score = 85 });
newList.Add(new ListItem { Name = "John", Score = 82 });
newList.Add(new ListItem { Name = "Steve", Score = 81 });
newList.Add(new ListItem { Name = "Roger", Score = 80 }); // Roger is a new entry
newList.Add(new ListItem { Name = "Phillip", Score = 79 }); // Philip moved down one
// Peter drops off this list with his score of 70, since I only want the top 10.
So the changes would be:
Update record 2 for "Steve", the score has changed
Insert new record "Roger" at position 9
Drop record for "Peter" off of the top 10.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您正在寻找通用的、与语言无关的解决方案,那么您正在寻找某种 有序列表的数据同步。基本算法是:
If you're looking for a general, language-agnostic solution, then you're looking for some kind of data synchronization of ordered lists. The basic algorithm would be:
您可以使用 Linq:
您的具体示例:
不确定它是否是最有效的,但它很简洁:)
Can you use Linq:
Your example specific:
Not sure if it is the most efficient but it's concise :)
如果您的列表中没有两次出现相同的名称,这应该能够解决问题。在您的示例中,您有 2 个 Steve,但您需要一种方法来区分他们。
你可以这样称呼它:
This should be able to do the trick if you don't have the same name twice in your list. In your example, you have 2x Steve, but you need a way to distinct between them.
You can call it like this: