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

看板Python作者 (想辦法突破)時間13年前 (2012/02/20 19:24), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串4/8 (看更多)
s=map(lambda x : set(x), target.values()) print(reduce(lambda x,y: x.intersection(y), s)) 以上這樣OK 有PRINT出正確結果 s=map(lambda x : set(x), target.values()) var_a = reduce(lambda x,y: x.intersection(y), s) print(var_a) 換成以上三行,想用一個變數去接 就會得到以下錯誤 var_a = reduce(lambda x,y: x.intersection(y), s) 錯誤訊息 : TypeError: reduce() of empty sequence with no initial value. ※ 引述《kadodo (想辦法突破)》之銘言: : 再請問一下 : 我想用一個變數來接 : 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 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.132.36.238
文章代碼(AID): #1FGYrq8F (Python)
討論串 (同標題文章)
文章代碼(AID): #1FGYrq8F (Python)