想象一下,你需要计算iterable
元素的数量,以满足一些criteria
- 是否有一个更好看的方式来做到这一点? 当然是python3。但2也很好。 我找不到合适的collections
或itertools
python模块或内置程序。python的len的快捷方式(列表(过滤器(lambda x:criteria,iterable)))
5
A
回答
3
从itertools
recipes尝试quantify
配方:
def quantify(iterable, pred=bool):
"Count how many times the predicate is true"
return sum(map(pred, iterable))
more_itertools
已经实现了这个配方,所以它的设计更加紧凑:
>>> import more_itertools as mit
>>> iterable = [True, False, True, True]
>>> mit.quantify(iterable)
3
对于比较:
>>> #len(list(filter(lambda x: criteria, iterable)))
>>> len(list(filter(lambda x: x is True, iterable)))
3
性能
# A: len(list(filter(lambda x: criteria, iterable)))
>>> %timeit -n 1000000 len(list(filter(lambda i: i is True, iterable)))
1000000 loops, best of 3: 2.48 µs per loop
# B: quantify(iterable, pred=condition)
>>> %timeit -n 1000000 mit.quantify(iterable)
1000000 loops, best of 3: 1.87 µs per loop
# C: ilen(item for item in iterable if condition)
>>> %timeit -n 1000000 mit.ilen(i for i in iterable if i is True)
1000000 loops, best of 3: 5.27 µs per loop
# D: len([item for item in iterable if condition])
>>> %timeit -n 1000000 len([i for i in iterable if i is True])
1000000 loops, best of 3: 973 ns per loop
# E: sum(1 for _ in iterable if condition)
>>> %timeit -n 1000000 sum(1 for i in iterable if i is True)
1000000 loops, best of 3: 1.34 µs per loop
- 一个:控制 - 作者的例子
- 乙:更快 -
quantify
,itertools
配方 - Ç:最慢 - 发电机表达,使用
more_itertools.ilen
评估 - d:最快 - 列表理解
- è:更快 - 发电机表达,
sum(1 for _ in ...)
成语
虽然more_itertools.quantify
简洁,独特的生成器表达式即使不是更快,也是平等的。然而,经典的列表解析(由@宏杰李首先提出)是表现最好的。
另请参阅将len()
扩展到发电机的thread。
1
列表解析提供了一种简洁的方式来创建列表。通用 应用程序将创建新列表,其中每个元素是 的一些操作的结果,该操作应用于另一个序列的每个成员或可迭代的 ,或者创建满足 特定条件的那些元素的子序列。
>>> squares = []
>>> for x in range(10):
... squares.append(x**2)
...
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
注意,这将创建(或覆盖)一个变量名为x循环完成后仍然存在 。我们可以通过计算平方 名单无任何毒副作用:
squares = list(map(lambda x: x**2, range(10)))
或等价:
squares = [x**2 for x in range(10)]
不仅仅是把你放在了conditon如果statment这样
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
你有符合条件的元素列表,你可以使用len(lsit_)
计数元素
3
sum(1 for i in A if condition(i))
相关问题
- 1. Python快捷方式
- 2. NetBeans快捷方式的确定列表
- 3. 快捷方式在Silverlight和默认浏览器快捷方式
- 4. 通过GPO的桌面快捷方式
- 5. 创建批处理和快捷方式的快捷方式
- 6. WiX快捷方式覆盖现有的同名快捷方式
- 7. Qt - 删除快捷方式 - 模糊的快捷方式超载
- 8. STL像容器typedef的快捷方式?
- 9. C#数据表过滤器最快的方式
- 10. ActiveRecord中的快捷方式?
- 11. Clojure:when-let的快捷方式
- 12. ComputerName中的快捷方式
- 13. 条件的快捷方式
- 14. Android的快捷方式
- 15. 是否有返回lambda中传递对象的快捷方式?
- 16. python cmd模块退出快捷方式
- 17. 在Python中模拟Windows快捷方式
- 18. 在Visual Studio中的文件列表的快捷方式
- 19. 在Java列表中的合并同类项的快捷方式
- 20. 使用表达式/ lambda比较/过滤两个列表的通用方法
- 21. 过滤A010784序列的最快方法
- 22. 如何将Iterable [Try [U]]过滤器成功转换为Iterable [U]?
- 23. 优化过滤逻辑Python的方式/从列表
- 24. 在HashMap中添加列表的快捷方式
- 25. 打开Windows 7任务栏跳转列表的快捷方式
- 26. 的列表属性C#快捷方式,不应该返回null
- 27. 从字典列表中仅打印1项的快捷方式
- 28. 在C中创建单个项目列表的快捷方式#
- 29. 如果len(列表)在Python
- 30. 如何通过python创建url快捷方式
也许''总和(标准(X)在迭代X)'',假设该标准()仅返回适当的布尔值。 – jasonharper
@jasonharper其实你的答案我最喜欢的 - 'sum(x <10和True for x in iterable)'超级方便:) – scythargon