【发布时间】:2012-08-16 01:01:28
【问题描述】:
在 C# 中,使用 LINQ,如果我有枚举 enumerable,我可以这样做:
// a: Does the enumerable contain an item that satisfies the lambda?
bool contains = enumerable.Any(lambda);
// b: How many items satisfy the lambda?
int count = enumerable.Count(lambda);
// c: Return an enumerable that contains only distinct elements according to my custom comparer
var distinct = enumerable.Distinct(comparer);
// d: Return the first element that satisfies the lambda, or throws an exception if none
var element = enumerable.First(lambda);
// e: Returns an enumerable containing all the elements except those
// that are also in 'other', equality being defined by my comparer
var except = enumerable.Except(other, comparer);
我听说 Python 的语法比 C# 更简洁(因此效率更高),那么我如何在 Python 中使用相同或更少的代码来实现相同的迭代?
注意:如果不需要,我不想将可迭代对象具体化为列表(Any、Count、First)。
【问题讨论】:
标签: python functional-programming iterator