看板 Python 關於我們 聯絡資訊
再請問一下 我想用一個變數來接 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
AlanSung:你的 s 真的有東西嗎? 02/20 18:28
kadodo:有 如果print出來會有東西 用var_a 去接的話才會出現錯誤 02/20 18:29