txt文件中每个记录占一行 A000000
A000001
A000002
A000003
A000004
A000002
A000005
A000006
...数据不超过5000行如何找出这些记录中重复的并将重复的删除.
例如上面记录中A000002 是重复的,则在这些记录中剔除A000002
A000001
A000002
A000003
A000004
A000002
A000005
A000006
...数据不超过5000行如何找出这些记录中重复的并将重复的删除.
例如上面记录中A000002 是重复的,则在这些记录中剔除A000002
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;
这还没怎么高效
只能搞笑
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;
只需要遍历一次就可以找出所有的重复数据。
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;
slstData.Sorted := True;
slstData.Duplicates := dupIgnore;
如果要求不改变原来的顺序,我的思路是:
把数据插入到一个排序的列表中,在插入过程中,使用算法(简单的如二分法)查找列表中是否存在,如果存在,把存在的行次记录到另外一个列表中,统计完成后,删除这些行次的数据
我用SQL自增列再交叉导出的数据,结果是一样,1秒
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;
点一下排序按钮就可以了。
弄过个小东西,分析一个txt文件,根据内容分类成多个新的txt文件,用的就这个方法感觉很快