txt文件中获取重复的记录

jzinfo 2009-09-10 01:03:59
txt文件中每个记录占一行

A000000
A000001
A000002
A000003
A000004
A000002
A000005
A000006
...

数据不超过5000行

如何找出这些记录中重复的并将重复的删除.
例如上面记录中A000002 是重复的,则在这些记录中剔除A000002
...全文
202 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
byteh 2009-09-11
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 kaikai_kk 的回复:]
Delphi(Pascal) codeprocedure TForm1.Button1Click(Sender: TObject);var
V: tstringlist;
i:Integer;begin
V:= tstringlist.Create;
V.LoadFromFile('d:\abc.txt');
i:=V.Count-1;if i=0then
Exit;w¡­
[/Quote]

弄过个小东西,分析一个txt文件,根据内容分类成多个新的txt文件,用的就这个方法感觉很快
纠结的程序猿 2009-09-11
  • 打赏
  • 举报
回复
kaikai_kk 2009-09-11
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
V: tstringlist;
i:Integer;
begin
V:= tstringlist.Create;
V.LoadFromFile('d:\abc.txt');
i:=V.Count-1;
if i=0 then
Exit;

while 1=1 do
begin
if V.IndexOf(V.Strings[i]) <> i then
V.Delete(i);
Dec(i);
if i=0 then
Break;
end;

V.SaveToFile('d:\abc.txt');
end;
弘石 2009-09-10
  • 打赏
  • 举报
回复
也许是我机器慢,我从1加到5000,用了5到6秒的时间
dinoalex 2009-09-10
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 youthon 的回复:]
引用 5 楼 dinoalex 的回复:
5000条,一秒

你用的测试数据重复率太高,你用5000个完全不一样的数据来测试,看看时间就知道了
[/Quote]

我用SQL自增列再交叉导出的数据,结果是一样,1秒
sanguomi 2009-09-10
  • 打赏
  • 举报
回复
卡庙的兄弟啊
弘石 2009-09-10
  • 打赏
  • 举报
回复
如果允许排序的话,简单的方法:
slstData.Sorted := True;
slstData.Duplicates := dupIgnore;
如果要求不改变原来的顺序,我的思路是:
把数据插入到一个排序的列表中,在插入过程中,使用算法(简单的如二分法)查找列表中是否存在,如果存在,把存在的行次记录到另外一个列表中,统计完成后,删除这些行次的数据
弘石 2009-09-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dinoalex 的回复:]
5000条,一秒
[/Quote]
你用的测试数据重复率太高,你用5000个完全不一样的数据来测试,看看时间就知道了
willflyz 2009-09-10
  • 打赏
  • 举报
回复
把你的文本内容Load到ListBox当中,ListBox.Sorted设置为true进行排序.先进行排序,再进行比较,这样效率会更高.

procedure TForm1.FormCreate(Sender: TObject);
begin
ListBox1.Clear;
ListBox1.Items.Add('2');
ListBox1.Items.Add('1');
ListBox1.Items.Add('3');
ListBox1.Items.Add('2');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
ListBox1.Sorted := True;
i := 0;
while (i < ListBox1.Count - 2) do
begin
if ListBox1.Items.Strings[i] = ListBox1.Items.Strings[i+1] then
begin
ListBox1.Items.Delete(i+1);
Continue;
end;
Inc(i);
end;
end;
mjp1234airen4385 2009-09-10
  • 打赏
  • 举报
回复
4楼的感觉可行。
Voidest 2009-09-10
  • 打赏
  • 举报
回复
这种方法10ms都用不到。
[Quote=引用 6 楼 voidest 的回复:]
开一个5000个大小的Boolean型数组,首先初始化为FALSE,查到一个数(忽略首字母A)就将对应的下标的值标记为TRUE,如果原来就是TRUE就是重复的,删除之。
只需要遍历一次就可以找出所有的重复数据。
[/Quote]
Voidest 2009-09-10
  • 打赏
  • 举报
回复
开一个5000个大小的Boolean型数组,首先初始化为FALSE,查到一个数(忽略首字母A)就将对应的下标的值标记为TRUE,如果原来就是TRUE就是重复的,删除之。
只需要遍历一次就可以找出所有的重复数据。
dinoalex 2009-09-10
  • 打赏
  • 举报
回复
5000条,一秒
dinoalex 2009-09-10
  • 打赏
  • 举报
回复
楼主的快接近了

[Code=Delphi(Pascal)]
procedure TForm1.Button1Click(Sender: TObject);
var
sl: tstringlist;
f: textfile;
s: string;
begin
sl:= tstringlist.Create;
AssignFile(f, 'd:\abc.txt');
reset(F);
while not eof(F) do
begin
Readln(F, s);
if sl.IndexOf(s) = -1 then
sl.Append(s);
end;
CloseFile(f);
sl.SaveToFile('d:\abc.txt');
sl.Free;
{abc.txt :
A000000
A000001
A000002
A000003
A000004
A000002
A000005
A000006
}
end;

[/Code]
QQ286251099 2009-09-10
  • 打赏
  • 举报
回复
打错了
把没字换 能字阅读
QQ286251099 2009-09-10
  • 打赏
  • 举报
回复
list.add('A000000 ');
list.add('A000002 ');
list.add('A000002 ');
list.add('A000001 ');
list.add('A000003 ');
for i:=0 to list.count -1 do begin
if blist.indexof(list[i]) < 0 then
blist.add(list[i])
end;
这还没怎么高效
只能搞笑
jzinfo 2009-09-10
  • 打赏
  • 举报
回复
高效算法的 大家提供一个看看

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧