统计项目频率

发布于 2024-11-14 23:05:15 字数 204 浏览 3 评论 0原文

您好,我正在使用 Delphi,我有一个包含以下项目的字符串列表:

45
A15
015
A15
A15
45

我想处理它并制作第二个字符串列表,其中包含 每个元素出现的次数:

45 [2]
015 [1]
A15 [3]

我如何用Delphi做到这一点?

Hi I'm using Delphi and I have a StringList with this items:

45
A15
015
A15
A15
45

I want to process it and make a second stringlist that will have
the number of appearance of each element:

45 [2]
015 [1]
A15 [3]

How can I do this with Delphi?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扮仙女 2024-11-21 23:05:15

您可以使用字典:

Frequencies := TDictionary <String, Integer>.Create;
try
  // Count frequencies
  for Str in StringList do
    begin
    if Frequencies.ContainsKey (Str) then
      Frequencies [Str] := Frequencies [Str] + 1
    else
      Frequencies.Add (Str, 1);
    end; 

   // Output results to console
   for Str in Frequencies.Keys do
     WriteLn (Str + ': ' + IntToStr (Frequencies [Str]));
finally
  FreeAndNil (Frequencies);
end;

唯一的问题可能是结果出现的顺序完全随机,并且取决于哈希映射的内部工作。

感谢 daemon_x 提供完整的单元代码:

program Project1;

{$APPTYPE CONSOLE}

uses SysUtils, Classes, Generics.Collections;

var Str: String;
    StringList: TStrings;
    Frequencies: TDictionary <String, Integer>;

begin
  StringList := TStringList.Create;

  StringList.Add('45');
  StringList.Add('A15');
  StringList.Add('015');
  StringList.Add('A15');
  StringList.Add('A15');
  StringList.Add('45');

  Frequencies := TDictionary <String, Integer>.Create;

  try
  // Count frequencies
  for Str in StringList do
    begin
      if Frequencies.ContainsKey (Str) then
        Frequencies [Str] := Frequencies [Str] + 1
      else
        Frequencies.Add (Str, 1);
    end;

   // Output results to console
   for Str in Frequencies.Keys do
     WriteLn (Str + ': ' + IntToStr (Frequencies [Str]));

finally
  StringList.Free;
  FreeAndNil(Frequencies);
end;

end.

You could use a dictionary:

Frequencies := TDictionary <String, Integer>.Create;
try
  // Count frequencies
  for Str in StringList do
    begin
    if Frequencies.ContainsKey (Str) then
      Frequencies [Str] := Frequencies [Str] + 1
    else
      Frequencies.Add (Str, 1);
    end; 

   // Output results to console
   for Str in Frequencies.Keys do
     WriteLn (Str + ': ' + IntToStr (Frequencies [Str]));
finally
  FreeAndNil (Frequencies);
end;

The only problem might be that the order in which the results appear is completely random and dependes on the inner working of the hash map.

Thanks to daemon_x for the full unit code:

program Project1;

{$APPTYPE CONSOLE}

uses SysUtils, Classes, Generics.Collections;

var Str: String;
    StringList: TStrings;
    Frequencies: TDictionary <String, Integer>;

begin
  StringList := TStringList.Create;

  StringList.Add('45');
  StringList.Add('A15');
  StringList.Add('015');
  StringList.Add('A15');
  StringList.Add('A15');
  StringList.Add('45');

  Frequencies := TDictionary <String, Integer>.Create;

  try
  // Count frequencies
  for Str in StringList do
    begin
      if Frequencies.ContainsKey (Str) then
        Frequencies [Str] := Frequencies [Str] + 1
      else
        Frequencies.Add (Str, 1);
    end;

   // Output results to console
   for Str in Frequencies.Keys do
     WriteLn (Str + ': ' + IntToStr (Frequencies [Str]));

finally
  StringList.Free;
  FreeAndNil(Frequencies);
end;

end.
り繁华旳梦境 2024-11-21 23:05:15
  1. 对原始列表进行排序,

    list1.sort;
    
  2. 创建新列表

    list2:=TStringList.Create;
    
  3. 迭代排序列表以计算每个不同的项目
    并将计数存储在结果列表的对象字段中(或者,如果您尚未使用它,只需将计数类型转换为指针并将其存储为对象)。

    previtem:=list1[0];
    计数:=1;
    对于 i:=1 到 list1.count-1 做
     开始
      如果 list1[i]=previtem 那么 
        包括(计数)
      别的
       开始
        list2.addObject(previtem,指针(count));
        previtem:=list1[i];
        计数:=1;
       结尾;
     结尾;
    list2.addObject(previtem,指针(count));
    

最后,再次迭代以将计数添加到字符串中

  for i:=0 to list2.count-1 do
    list2.items[i]:=list2[i]+' ['+inttostr(list2.objects[i])+']';
  1. Sort the original list,

    list1.sort;
    
  2. create a new list

    list2:=TStringList.Create;
    
  3. iterate over the sorted list to count every different item
    and store the a count in the objects field of the resulting list (or if you don't use it already, just typecast the count into a pointer and store it as the object).

    previtem:=list1[0];
    count:=1;
    for i:=1 to list1.count-1 do
     begin
      if list1[i]=previtem then 
        inc(count)
      else
       begin
        list2.addObject(previtem,pointer(count));
        previtem:=list1[i];
        count:=1;
       end;
     end;
    list2.addObject(previtem,pointer(count));
    

finally, iterate again to add the count to the string

  for i:=0 to list2.count-1 do
    list2.items[i]:=list2[i]+' ['+inttostr(list2.objects[i])+']';
浪荡不羁 2024-11-21 23:05:15

我在我的头上编写了这个代码,因为我现在还没有安装 Delphi。让我知道它对您有何作用。
Stringlist1 是包含项目的原始列表,stringlist2 为空,将用于存储您想要的内容。

for i := 0 to stringlist1.Count - 1 do
begin
    if (stringlist2.Values[stringlist1[i]] = '') then
        stringlist2.Values[stringlist1[i]] := '1'
    else
        stringlist2.Values[stringlist1[i]] :=
            IntToStr(StrToInt(stringlist2.Values[stringlist1[i]]) + 1);
end;

I coded this on my head as I don't have Delphi installed as of now. Let me know how it works for you.
Stringlist1 is the original list with the items, stringlist2 is empty and will be used to store what you want.

for i := 0 to stringlist1.Count - 1 do
begin
    if (stringlist2.Values[stringlist1[i]] = '') then
        stringlist2.Values[stringlist1[i]] := '1'
    else
        stringlist2.Values[stringlist1[i]] :=
            IntToStr(StrToInt(stringlist2.Values[stringlist1[i]]) + 1);
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文