由于没有提到只是为了完成。 好olfilter来过滤你的过滤元素。
函数式编程ftw。
####### Set Up ####### class X: def __init__(self, val): self.val = val elem = 5 my_unfiltered_list = [X(1), X(2), X(3), X(4), X(5), X(5), X(6)] ####### Set Up ####### ### Filter one liner ### filter(lambda x: condition(x), some_list) my_filter_iter = filter(lambda x: x.val == elem, my_unfiltered_list) ### Returns a flippin' iterator at least in Python 3.5 and that's what I'm on print(next(my_filter_iter).value) print(next(my_filter_iter).value) print(next(my_filter_iter).value) ### [1, 2, 3, 4, 5, 5, 6] Will Return: ### # 5 # 5 # Traceback (most recent call last): # File "C:\Users\mousavin\workspace\Scripts\test.py", line 22, in # print(next(my_filter_iter).value) # StopIteration # # You can do that None stuff or whatever at this point, if you don't like exceptions.
我知道,通常在Python列表parsing是首选或至less这是我读的,但我不认为这个问题是诚实的。 当然,Python不是一种FP语言,但Map / Reduce / Filter是完全可读的,是function性编程中最标准的标准用例。
所以你去了 知道你的function编程。
过滤条件列表
它不会比这更容易:
next(filter(lambda x: x.value == value, test_list)