假设我在列表中有元素(X、Y 和 Z),我有一个函数,可以生成两个对象彼此相似程度的百分比。
我想做的是使用我的compareElements对Y和Z运行X,所以:
compareElements(X,Y); // equals 55
compareElements(X,Z); // equals 60
然后Y对X和Z
compareElements(Y,X); // equals 55
compareElements(Y,Z); // equals 62
然后Z对Y和X
compareElements(Z,X); // equals 60
compareElements(Z,Y); // equals 62
然后,我返回最高值,即62。
显然,那里有一些重复,我不需要重复,但我不知道如何消除它。
如何构建 LINQ 查询或函数/算法来对每个元素进行比较而不重复?
如果可以的话,我更愿意使用 LINQ,因为我正在传递一个可枚举值,并且该函数在实际枚举列表之前返回,因此我们可以节省执行比较的成本,直到枚举列表为止。
我所需要的只是比较函数的最高值 62。
注意:我的实际结果集我正在处理列表中 3 到 10 个元素之间的平均值,需要通过此比较函数运行这些平均值。< /em>
Say I have elements (X, Y, and Z) in a list, I have a function, that generates a percentage, of how much two objects resemble each other.
What I want to do, is run X against Y and Z using my compareElements, so:
compareElements(X,Y); // equals 55
compareElements(X,Z); // equals 60
Then Y against X and Z
compareElements(Y,X); // equals 55
compareElements(Y,Z); // equals 62
Then Z against Y and X
compareElements(Z,X); // equals 60
compareElements(Z,Y); // equals 62
Then, I return the highest value, which is 62.
Obviously, there's some repetition there, I don't need the repetition, but I'm not sure how to eliminate it.
How do I structure my LINQ query, or a function/algorithm to do this comparison on every element, without the repetition?
I'd prefer to use LINQ if I can, as I'm being passed an enumerable and the function returns before the list is actually enumerated, so we can save the cost of performing the compare, until the list is enumerated.
All I need is that highest value, of the compare functions, 62.
Note: My actual result set I'm working with averages between 3 and 10 elements in the list, that need to be ran through this compare function.
发布评论
评论(7)
我倾向于这样做:
I'd be inclined to do it like this:
我不确定我是否正确理解你,但尝试这样的事情:
这将为你消除重复的比较。它不使用 LINQ,但我认为它仍然具有很好的可读性。
更新:这是我修改后的版本以处理 IEnumerables。它与 Jon Hanna 的不同之处在于它不创建新列表,而只是跟踪两个迭代器。
I'm not sure I'm understanding you correctly, but try something like this:
This will eliminate the duplicate comparisons for you. It doesn't use LINQ, but I think it's still pretty readable.
UPDATE: Here is my version modified to handle IEnumerables. It varies from Jon Hanna's in that it doesn't create a new List, it just keeps track of two iterators.
为了便于阅读,我会编写一个迭代器块以非重复的方式生成比较:
然后您可以执行以下操作:(
这就是说,对于没有实际理由使用 LINQ 或迭代器的情况,我更喜欢 Smelch 的建议,例如需要可组合例程。)
For the sake of readability, I would write an iterator block to generate the comparisons in a non-repetitive manner:
Then you can do the following:
(That said I prefer Smelch's suggestion for situations where there's no practical reason to use LINQ or iterators, such as having a need for composable routines.)
不知道这是否是您要搜索的内容,但我会尝试以这种方式使用 LINQ:
比较示例实现:
通过这种方式,您可以将每个元素与列表中的其他元素进行比较(我认为这是一种排列) )除自身外,然后选择比较值,最后选择最高值。
Don't know if it is what you are searching for, but I would try to use LINQ in this way:
Comparison sample implementation:
This way you compare each element against the other elements in the list (it is a sort of permutation I think) except itself, then select the comparison value and finally the highest value.
您可以将要测试的可能组合列表编译为
List>
然后选择最大值
You could compile a list of the possible combinations you want to test into a
List<Tuple<int, int>>
and then select the maximum
与给出的一对几乎相同,但不需要首先分配给列表。
Pretty much the same as a couple given, but doesn't require one to assign to a list first.
不可读的 LINQ 实现(可能无法编译,我没有测试过):
An unreadable LINQ implementation (may not compile, I haven't tested):