【问题标题】:Intersection of two strings/ sets两个字符串/集合的交集
【发布时间】:2012-10-22 18:52:19
【问题描述】:

由于来自 python,我正在寻找与 delphi5 中的这个 python 代码 (sets) 等效的东西:

>>> x = set("Hello")
>>> x
set(['H', 'e', 'l', 'o'])

>>> y = set("Hallo")
>>> y
set(['a', 'H', 'l', 'o'])

>>> x.intersection(y)
set(['H', 'l', 'o'])

【问题讨论】:

    标签: delphi char set intersection delphi-5


    【解决方案1】:
    var
      a, b, c: set of byte;
    begin
      a := [1, 2, 3, 4];
      b := [3, 4, 5, 6];
      c := a*b;          // c is the intersection of a and b, i.e., c = [3, 4]
    

    但要小心:

    var
      a, b, c: set of integer;
    

    甚至不会编译;相反,您会收到“集合可能最多有 256 个元素”错误。有关 Delphi 集的更多信息,请参阅documentation

    更新

    抱歉,忘了提到“显而易见”(从 Delphi 程序员的角度来看):

    var
      a, b, c: set of char;
    begin
      a := ['A', 'B', 'C', 'D'];
      b := ['C', 'D', 'E', 'F'];
      c := a*b;          // c is the intersection of a and b, i.e., c = ['C', 'D']
    

    但是你的字符都将是字节字符——也就是说,忘记 Unicode(Delphi 5 不支持 Unicode,所以在这种情况下这并不是真正的限制)!

    【讨论】:

    • 在 Delphi 5 中,我会在这里选择 set of char
    • @David:当然,在 OP 的情况下,'char' 更相关。不过,我认为原理很明确。但是,当然,OP 必须意识到集合中的“字符”不能是 Unicode...
    • 对,在 Delphi 5 中,Char=AnsiChar 恰好不是问题
    • @David:哦,我什至没看到(没有标签!)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多