python中属性赋值查找_在列表中查找具有等于某个值的属性(符合任何条件)

由于没有提到只是为了完成。 好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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值