Re: [問題]不用for迴圈尋找陣列中只出現過一次的資料

看板Python作者 (Baby baby~~)時間11年前 (2014/05/12 04:57), 編輯推噓1(103)
留言4則, 1人參與, 最新討論串4/5 (看更多)
這應該是個 map reduce 的題目吧 l = [2, 3, 4, 5, 0, 1, 2, 3, 4, 2, 3, 5] def func_map(a): return [set([a]), set([a])] def func_reduce(am, bm): uaset, aset = am ubset, bset = bm unset = (uaset - bset) | (ubset - aset) nset = aset | bset return [unset, nset] reduce(func_reduce, map(func_map, l)) 先把值 map 成 uniq set, all number set 再 reduce 成 uniq set, all number set -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 218.161.71.110 ※ 文章網址: http://www.ptt.cc/bbs/Python/M.1399841851.A.25C.html

05/12 11:29, , 1F
出現啦!!!
05/12 11:29, 1F

05/12 11:50, , 2F
``set([a])`` 可以寫成 ``{a}``
05/12 11:50, 2F

05/12 11:50, , 3F
``return [unset, nset]`` 可以寫成 ``return unset, nset``
05/12 11:50, 3F

05/12 11:50, , 4F
會更精簡些
05/12 11:50, 4F
文章代碼(AID): #1JR-Gx9S (Python)
文章代碼(AID): #1JR-Gx9S (Python)