2016-08-20 84 views
1

更新解決了編譯錯誤,現在唯一的問題是代碼是如何按照字母順序排序字典以獲得漂亮的打印效果。如何排序精靈中的字典

我正在重構一個argument parser從python到Genie,但是我發現自己陷入瞭如何在將項目附加到列表之前對它們進行排序。

lines.append("Options:") 
    if len(self.options): 
     for name, option in sorted(self.options.items()): 
      lines.append(" %s: %s" % (name, option.values)) 
    else: 
     lines.append(" [none]") 

self.options被聲明爲self.options = {}

現在怎麼可以打印字典的內容,但排序:

在蟒蛇,因爲它是那麼簡單?

這裏就是我堅持的代碼:

def ListOptions() 
    var lines = new list of string 

    lines.add("Options:") 
    if _options.size != 0 
     for name in _options.keys 
      lines.add(" %s: %s" % (name, _options.values)) 
    else 
     lines.add(" [none]") 

ListOptions是一個類中的一個方法,我宣佈_options爲_options:字符串的新字典,串

沒有編譯錯誤在代碼的那一部分中。我的問題是如何在將字典添加到列表lines之前對字典的元素進行排序?

+0

當你說「參數解析器」時,你是指從命令行傳遞給程序的參數嗎? – AlThomas

+0

我的意思是,如[這裏](https://github.com/dmulholland/clio)...但我猜我試圖實現無關這個問題,我面對...的問題是如何迭代已按字母順序排序的字典... –

+1

如果您正在尋找解析命令行參數,您應該查看GLib的OptionContext和OptionEntry。這對大多數目的都很有用。對於瓦拉的例子看http://stackoverflow.com/questions/33431446/how-to-do-optioncontext-parsing-on-an-instance – AlThomas

回答

2

根據Thomas和Jens的評論,也可以使用TreeMap。下面是它的外觀:

[indent=4] 

uses 
    Gee 

init 
    var dic = new TreeMap of string, string 
    dic["z"] = "23" 
    dic["abc"] = "42" 
    dic["pi"] = "3.141" 
    for k in dic.ascending_keys 
     print (@"$k: $(dic[k])") 
2

A dict of實際上是Gee.HashMap of K, V,因此您可以查找keys屬性是什麼類型。

keysGee.Set of G類型不具有一種方法的。

但它確實從Gee.Collection of G,我們可以使用,使派生新的臨時list of string(這是引擎蓋下Gee.ArrayList確實有一個sort方法)。

我已經把它放到sort_string_collection函數中(它甚至可以是泛型的,因爲它不是特定於字符串的,但我沒有打擾,因爲it's not easily possible with Genie at the moment)。

隨着加入的測試代碼,使之成爲MCVE,結果是這樣的:

[indent=4] 

def sorted_string_collection (collection: Gee.Collection of string): Gee.Iterable of string 
    var l = new list of string 
    l.add_all (collection); 
    l.sort() 
    return l; 

def list_options (_options: dict of string, string): list of string 
    var lines = new list of string 

    lines.add("Options:") 
    if _options.size != 0 
     for name in sorted_string_collection (_options.keys) 
      lines.add(@" $name: $(_options[name])") 
    else 
     lines.add(" [none]") 

    return lines 

init 
    var opts = new dict of string, string 
    opts["z"] = "23" 
    opts["abc"] = "42" 
    opts["pi"] = "3.141" 
    var l = list_options (opts) 
    for var s in l 
     print (s) 

甚至更​​簡約(如果我們永遠能使用精靈計算器文檔,這將是一個很好的例子):

[indent=4] 

def sorted_string_collection (collection: Gee.Collection of string): Gee.Iterable of string 
    var l = new list of string 
    l.add_all (collection); 
    l.sort() 
    return l; 

init 
    var dic = new dict of string, string 
    dic["z"] = "23" 
    dic["abc"] = "42" 
    dic["pi"] = "3.141" 
    for k in sorted_string_collection (dic.keys) 
     print (@"$k: $(dic[k])") 
+1

如果你想有效的搜索鍵,鍵和值的值,然後排序Gee的TreeMap更好。 TreeMap實現Gee。SortedMap可以同時獲得'ascending_keys'和'ascending_entries'屬性。 – AlThomas

+0

的確如此,選擇最合適的數據結構總是明智的選擇。你應該考慮將你的評論轉換爲答案,因爲它是我的另一種選擇。 –

+0

謝謝你們兩位! @在print(@ ...)和lines_add(@ ...)中的作用是什麼? –