2017-10-19 128 views
3

在Python中,可以很容易地創建如何在禮拜堂中表示集或詞典?

>>> s = set() 
>>> s.add("table") 
>>> s.add("chair") 
>>> s.add("emu") 
>>> s 
set(['emu', 'table', 'chair']) 

了一套獨特的,未排序的對象我知道教堂有域,但它是正確的使用這些作爲套?有什麼問題嗎?字典怎麼樣?

回答

5

並非所有的教堂域集,但「關聯域」可以作爲集:

var s : domain(string); 
s.add("table"); 
s.add("chair"); 
s.add("emu"); 
writeln(s); // {chair, table, emu} 
var t = {"table", "chair", "emu"}; // associative domain literal 

像Python套,聯想域的支持成員的檢查和工會,差集,交集操作(等等)。 See the online docs for more information.默認情況下,關聯域可以並行使用。

Chapel's'associative arrays'與python字典類似。教堂陣列是從索引到元素的映射,所以我們可以創建一個具有關聯域的關聯數組:

var inds = {1, 2, 3, 7, 42}; 
var map : [inds] string; 

map[3] = "foo"; 
map[42] = "bar"; 

inds.add(100); // add new index and element 
assert(map[100] == ""); 
map[100] = "baz"; 

var lit = ["bob" => 1, "alice" => 2]; // assoc. array literal