在 C# 中,使用 LINQ,如果我有 enumeration 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
),我不想将可迭代对象具体化为列表。