Trong bài này ta cần tìm số trong list xuất hiện số lần lẻ. Ta cũng yêu cầu dùng hàm Lambda và reduce.
Ta thiết kế một hàm nơi reduce được dùng bằng cách áp dụng Lambda để kiểm tra nếu phần tử xuất hiện số lần lẻ.
Ví dụ
from functools import reduce def oddcount(i): print(reduce(lambda x, y: x ^ y, i)) listA = [12,34,12,12,34] print("Given list:\n",listA) print("The element present odd number of times:") oddcount(listA)Đầu ra
Chạy mã trên cho kết quả sau:
Given list: [12, 34, 12, 12, 34] The element present odd number of times: 12