Re: [問題] 如何將dict的values取set
建議你把完整的code從頭到尾貼上來,或是直接找個空間上傳你的檔案
另外
(lambda x: set(x))==set
(lambda x,y: x.intersection(y))==set.intersection
這裡用lambda非常多餘
加上set.intersection本身就可以接受多個參數
因此寫成set.intersection(*map(set, target.values()))就好
※ 引述《kadodo (想辦法突破)》之銘言:
: 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.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.217.2
推
02/21 15:29, , 1F
02/21 15:29, 1F
→
02/21 15:30, , 2F
02/21 15:30, 2F
→
02/21 18:03, , 3F
02/21 18:03, 3F
→
02/21 18:14, , 4F
02/21 18:14, 4F
推
02/21 18:40, , 5F
02/21 18:40, 5F
推
02/21 19:37, , 6F
02/21 19:37, 6F
→
02/21 21:08, , 7F
02/21 21:08, 7F
→
02/21 21:10, , 8F
02/21 21:10, 8F
→
02/21 21:11, , 9F
02/21 21:11, 9F
推
02/21 22:45, , 10F
02/21 22:45, 10F
→
02/23 01:15, , 11F
02/23 01:15, 11F
→
02/23 01:17, , 12F
02/23 01:17, 12F
→
02/23 18:06, , 13F
02/23 18:06, 13F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 5 之 8 篇):
Python 近期熱門文章
PTT數位生活區 即時熱門文章