作者ioptt (ioptt)
看板Python
標題[問題] dictionary<string,dictionary<string,
時間Thu Mar 31 15:23:07 2016
各位前輩, 想請問
class1(string name, string type, string comt)
Dictionary<string(module), Dictionary<string(name), class1>>
的問題
我需要從檔案中讀取資料, 想存在如上的資料結構裡, 以方便做search
由於檔案的內容長度不一定, 所以想利用dictionary的特性.
當我抓到第一組資料時, ex:
module1: name0, type0, comt0
name1, type1, comt1
name2, type2, comt2
....
nameN, typeN, comtN
module2: name0, type0, comt0
name1, type1, comt1
....
nameM, typeM, comtM
.......
moduleK: name0, type0, comt0
name1, type1, comt1
....
nameL, typeL, comtL
由於幾個module是不一定, 每個module裡有幾組name/type/comt也不一定.
我想要做成一個方便資料搜索的dictionary
當我想搜尋module2時, 會去找module2裡的dictionary,
然後去找nameX, 然後資料會找到nameX, typeX, comtX
我現在碰到的問題是:
由於不同模組下的name, 有可能會相同, 但type和comt可能會不同,
且我不知道會有幾個module, 所以我要怎麼動態去產生 不同module下的dictionary.
請教各位前輩高手了. 謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.61.209.67
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1459408990.A.5F3.html
推 tiefblau: 兩層dict或是用tuple(module_name, name)當成key 03/31 15:42
推 largesperm: 如果你的 module 名稱不一樣 你下面的 dict key 一樣 03/31 17:40
→ largesperm: 也沒差吧 上層不一樣就可以了 03/31 17:41
→ largesperm: 第二層甚至不用 dict 用 list 就好了 反正都固定 03/31 17:42
→ largesperm: 第二層要 dict 也是可以 03/31 17:43
→ largesperm: {mod1:{name0:[type0,comt0]}, mod2:{name0:[type0,co 03/31 17:44
→ largesperm: mt0]}} 03/31 17:44
→ largesperm: 儘管在 mod1 跟 mod2 dict 都有 name0 也沒差 03/31 17:44
→ largesperm: 反正是在不同的 dict(mod1,mod2) 03/31 17:45