- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
事实证明,这比我想象的要难。基本上,系统每天都会将客户主列表的快照转储到 CSV 中。它包含大约 120000 条记录和 60 个字段。大约 25mb。无论如何,我想报告在一个快照和另一个快照之间发生变化的值。它不是计划文件差异,因为它必须与包含客户唯一编号的最左边的列值相匹配。可以插入/删除行等。所有字段都是字符串,包括引用号。
我已经使用 LINQ 编写了一个解决方案,但它因数据集较大而失效。对于 10000 条记录,需要 17 秒。 120000,两个文件对比,用了将近2个小时。现在它使用优秀且免费的“filehelpers”http://www.filehelpers.com/加载数据,这只需要几秒钟,然后。但是检测哪些记录发生了变化是比较有问题的。以下是 2 小时的查询:
var changednames = from f in fffiltered
from s in sffiltered
where f.CustomerRef == s.CustomerRef &&
f.Customer_Name != s.Customer_Name
select new { f, s };
您会推荐什么方法?我想立即将列表“修剪”为那些有某种变化的列表,然后将我更具体的比较应用于那个小子集。我的一些想法是:
a) 使用字典或哈希集——尽管早期测试并没有真正显示出改进
b) 划分操作 - 使用客户引用字段中的第一个字符,并仅与具有相同字符的字符匹配。这可能涉及创建许多单独的集合,而且看起来很不雅观。
c) 放弃类型化数据排列并使用数组来实现。同样, yield 不确定。
有什么想法吗?
谢谢!
最佳答案
出于下面讨论的目的,我假设您有某种方法可以将 CSV 文件读入类中。我会调用那个类(class)MyRecord
.
将文件加载到单独的列表中,将它们命名为NewList
和 OldList
:
List<MyRecord> NewList = LoadFile("newFilename");
List<MyRecord> OldList = LoadFile("oldFilename");
使用 LINQ 执行此操作可能有更优雅的方法,但想法是直接合并。首先,您必须对这两个列表进行排序。您的 MyRecord
类(class)工具IComparable
,或者您提供自己的比较代表:
NewList.Sort(/* delegate here */);
OldList.Sort(/* delegate here */);
如果MyRecord
,您可以跳过委托(delegate)工具 IComparable
.
现在是直接合并。
int ixNew = 0;
int ixOld = 0;
while (ixNew < NewList.Count && ixOld < OldList.Count)
{
// Again with the comparison delegate.
// I'll assume that MyRecord implements IComparable
int cmpRslt = OldList[ixOld].CompareTo(NewList[ixNew]);
if (cmpRslt == 0)
{
// records have the same customer id.
// compare for changes.
++ixNew;
++ixOld;
}
else if (cmpRslt < 0)
{
// this old record is not in the new file. It's been deleted.
++ixOld;
}
else
{
// this new record is not in the old file. It was added.
++ixNew;
}
}
// At this point, one of the lists might still have items.
while (ixNew < NewList.Count)
{
// NewList[ixNew] is an added record
++ixNew;
}
while (ixOld < OldList.Count)
{
// OldList[ixOld] is a deleted record
}
只有 120,000 条记录,执行起来应该非常快。如果合并花费的时间与从磁盘加载数据一样长,我会感到非常惊讶。
编辑:LINQ 解决方案
我在思考如何使用 LINQ 来实现这一点。我不能做与上面的合并完全相同的事情,但我可以在单独的集合中获取添加、删除和更改的项目。
为此,MyRecord
必须实现IEquatable<MyRecord>
并覆盖 GetHashCode
.
var AddedItems = NewList.Except(OldList);
var RemovedItems = OldList.Except(NewList);
var OldListLookup = OldList.ToLookup(t => t.Id);
var ItemsInBothLists =
from newThing in NewList
let oldThing = OldListLookup[newThing.Id].FirstOrDefault()
where oldThing != null
select new { oldThing = oldThing, newThing = newThing };
在上面,我假设 MyRecord
有一个 Id
独特的属性。
如果您只想要更改的项目而不是两个列表中的所有项目:
var ChangedItems =
from newThing in NewList
let oldThing = OldListLookup[newThing.Id].FirstOrDefault()
where oldThing != null && CompareItems(oldThing, newThing) != 0
select new { oldThing = oldThing, newThing = newThing };
假设 CompareItems
方法将对两项进行深入比较,如果比较相等则返回 0,如果发生变化则返回非零。
关于c# - 使用 C# 高效地识别 CSV 文件中更改的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5240218/
我有一个功能是转换 ADO Recordset 进入html: class function RecordsetToHtml(const rs: _Recordset): WideString; 该函
经过几天的研究和讨论,我想出了这种方法来收集访客的熵(你可以看到我的研究历史here) 当用户访问时,我运行此代码: $entropy=sha1(microtime().$pepper.$_SERVE
给定一个无序列表 List ,我需要查找是否存在 String与提供的字符串匹配。 所以,我循环 for (String k : keys) { if (Utils.keysM
我已经搜索过这个问题,但没有找到我正在寻找的答案。 基本上,我想将类构造函数包装在 try/except 子句中,以便它忽略构造函数内特定类型的错误(但无论如何都会记录并打印它们)。我发现做到这一点的
我有一组三个数字,我想将一组数字与另一组数字进行比较。即,第一组中的每个数字小于另一组中的至少一个数字。需要注意的是,第一组中的下一个数字必须小于第二组中的不同数字(即,{6,1,6} 对 {8,8,
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
首先介绍一下背景: 我正在开发一个带有 EJB 模块和应用程序客户端模块的企业应用程序 (ear)。我还使用 hibernate JPA 来实现持久性,并使用 swingx 来实现 GUI。这些是唯一
我正在尝试在我的上网本上运行 Eclipse 以便能够为 Android 进行开发。 您可能已经猜到了,Eclipse 非常慢,并且不容易有效地开发。 我正在使用 Linux Ubuntu 并且我还有
for row, instrument in enumerate(instruments): for col, value in enumerate(instrument):
return not a and not b ^ 我如何以更好的格式表达它 最佳答案 DeMorgan's Law , 也许? return not (a or b) 我认为在这一点上已经足够简单了
我正在尝试让 Font Awesome 图标看起来更 slim https://jsfiddle.net/cliffeee/7L6ehw9r/1/ . 我尝试使用“-webkit-text-strok
假设我有一个名为 vals 的数据框,如下所示: id…………日期…………min_date…… .........最大日期 1…………2016/01/01…………2017/01/01…………2018/
是否有更 Pythonic 的方式来做到这一点?: if self.name2info[name]['prereqs'] is None: se
我有一个函数可以将一些文本打印到它接收到的 ostream&。如果 ostream 以终端为目标,我想让它适应终端宽度,否则默认为某个值。 我现在做的是: 从 ostream 中获取一个 ofstre
这个问题在这里已经有了答案: Should a retrieval method return 'null' or throw an exception when it can't produce
我有这个 bc = 'off' if c.page == 'blog': bc = 'on' print(bc) 有没有更 Pythonic(和/或更短)的方式在 Python 中编写? 最佳
输入:一个包含 50,000 行的 CSV;每行包含 910 列值 0/1。 输出:运行我的 CNN 的数据框。 我编写了一个逐行读取 CSV 的代码。对于每一行,我将数据分成两部分,称为神经元(90
据我所知,with block 会在您退出 block 后自动调用 close(),并且它通常用于确保不会忘记关闭一个文件。 好像没有技术上的区别 with open(file, 'r+') as f
我有一个使用 Entity Framework V6.1.1 的 MVC 5 网站。 Entity Framework DbContext 类和模型最初都在网站项目中。这个项目有 3 个 DbCont
我是编程新手,在尝试通过将 tableView 和关联 View 的创建移动到单独的类并将委托(delegate)和数据源从 VC 移动到单独的类来精简我的 ViewController 时遇到了一些
我是一名优秀的程序员,十分优秀!