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

看板Python作者 (想辦法突破)時間13年前 (2012/02/20 18:17), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串3/8 (看更多)
再請問一下 我想用一個變數來接 var_a = reduce(lambda x,y: x.intersection(y), s) TypeError: reduce() of empty sequence with no initial value. 請問該如何解決? 我用Help去看 還是看不懂 reduce(function, sequence[, initial]) -> value Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. ※ 引述《kadodo (想辦法突破)》之銘言: : 哈囉 我使用這兩行code, 執行後得到以下的NameError, 請問該如何解決? : 先謝謝啦 : s=map(lambda x : set(x), target.values()) : print(reduce(lambda x,y: x.intersection(y), s)) : NameError: global name 'reduce' is not defined : ※ 引述《kadodo (想辦法突破)》之銘言: : : 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 18:28, , 1F
你的 s 真的有東西嗎?
02/20 18:28, 1F

02/20 18:29, , 2F
有 如果print出來會有東西 用var_a 去接的話才會出現錯誤
02/20 18:29, 2F
文章代碼(AID): #1FGXt7tG (Python)
討論串 (同標題文章)
文章代碼(AID): #1FGXt7tG (Python)