[問題] 如何將dict的values取set

看板Python作者 (想辦法突破)時間13年前 (2012/02/20 15:04), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/8 (看更多)
target = {'a': [ab, cd, ef], 'b': [ac, cd, ef, gh], 'c': [bh, ef, cd, jk], ...} 假設target還有很多keys... 請問如何將 所有的 values of target取 set 以我舉的例子 我想得到 一個 set (cd, ef) 請教一下code該怎麼寫呢? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 192.203.57.28

02/20 16:23, , 1F
set有個function叫intersection
02/20 16:23, 1F

02/20 17:07, , 2F
s = map (lambda x: set(x), target.values())
02/20 17:07, 2F

02/20 17:07, , 3F
print reduce (lambda x, y: x.intersection(y), s)
02/20 17:07, 3F
文章代碼(AID): #1FGV2Hy_ (Python)
文章代碼(AID): #1FGV2Hy_ (Python)