2012-10-22 42 views
4

由於蟒蛇來我正在尋找在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']) 

回答

8
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(德爾福5不支持Unicode,所以在這種情況下,這不是一個限制)!

+0

在德爾福5中,我會去'這裏設置char' –

+0

@David:當然,在OP的例子中,'char'更相關。不過,我認爲這個原則很明確。但是,當然,OP必須認識到一個集合中的'char'不能是Unicode ... –

+0

對,並且在Delphi 5中使用'Char = AnsiChar',這不會是個問題 –