从列表中删除符合索引条件的元素

huangapple go评论62阅读模式
英文:

Removing elements from a List which respect a condition on the index

问题

Here's the translated code portion:

我被要求创建一个函数,该函数根据指定条件从整数列表中删除所有元素,即元素是其索引的倍数。

使用迭代和类似的方法很容易做到这一点,但我想以更“巧妙”的方式来做,以进一步深入编写代码、Linq  Lambdas

这是我尝试过的代码:

public static List<int> MultipleOfIndex(List<int> xs) =>
    xs.RemoveAll(s => (s % xs.IndexOf(s) == 0));

但我收到以下错误:错误 CS0029:无法隐式将类型 'int' 转换为 'System.Collections.Generic.List<int>'

我实际上无法理解在哪里进行了隐式转换,因为我的逻辑是这样的:我想删除所有满足 s 模除其索引为 0 的元素。
我希望能够得到解释,还希望看到如何使用 Where/Select 子句来实现它。

提前感谢!
英文:

I'm asked to create a function which removes all the elements from a list of integers respecting a specified condition i.e. the element is multiple of his index.

It is very easy to do it with iterations and things like that but I wanted to do it in a "cuter" way just to get into more complex writing codes, Linq and Lambdas.

That's what I've tried

public static List&lt;int&gt; MultipleOfIndex(List&lt;int&gt; xs) =&gt;
    xs.RemoveAll(s =&gt; (s % xs.IndexOf(s) == 0));

but I get the following error: error CS0029: Cannot implicitly convert type 'int' to 'System.Collections.Generic.List<int>'

I can't really understand where I'm doing an implicit conversion since my logic is this: I want to remove all the elements s where s modulo his index is 0.
I'd like to have an explanation and also I'd like to see how can I also implement it with Where/Select clauses.

Thanks in advance!

答案1

得分: 1

如果你想修改原始列表,你可以使用以下方法(注意你需要处理除零的情况):

public static List<int> MultipleOfIndex(List<int> xs)
{
    xs.RemoveAll(s => xs.IndexOf(s) == 0 && s != 0 || s % xs.IndexOf(s) == 0);
    return xs;
}

更高效的版本可能是:

public static List<int> MultipleOfIndex(List<int> xs)
{
    for (var i = xs.Count - 1; i >= 0; i--)
    {
        if(i == 0 && xs[i] != 0 || xs[i] % i == 0)
            xs.RemoveAt(i);
    }
    return xs;
}

如果你想创建一个新的列表,你可以使用LINQ:

public static List<int> MultipleOfIndex(List<int> xs) =>
    xs.Where((s, index) => index == 0 && s == 0 || s % index != 0).ToList();
英文:

If you want to modify the original list, you can use the following (note that you need to handle a divide-by-zero)

public static List&lt;int&gt; MultipleOfIndex(List&lt;int&gt; xs)
{
    xs.RemoveAll(s =&gt; xs.IndexOf(s) == 0 &amp;&amp; s != 0 || s % xs.IndexOf(s) == 0);
    return xs;
}

A more efficient version might be:

public static List&lt;int&gt; MultipleOfIndex(List&lt;int&gt; xs)
{
    for (var i = xs.Count - 1; i &gt;= 0; i--)
    {
        if(i == 0 &amp;&amp; xs[i] != 0 || xs[i] % i == 0)
            xs.RemoveAt(i);
    }
    return xs;
}

If you want to create a new list, you can use LINQ

public static List&lt;int&gt; MultipleOfIndex(List&lt;int&gt; xs) =&gt;
    xs.Where((s, index) =&gt; index == 0 &amp;&amp; s == 0 || s % index != 0).ToList();

</details>



# 答案2
**得分**: 0

我认为将列表映射到包含索引和值的元组列表会更好,其中第一个元素是索引,第二个元素是值,原因是 `removeAll()` 可能是并行的,从而使算法不一致。这样我认为你也不会遇到问题(尽管我没有尝试过这种方法)。

<details>
<summary>英文:</summary>

I think it would be better to map the list to a list of tuples, with the first element being the index and the second the value, the reason being that `removeAll()` might be parallel, thusly making the algorithm inconsistent. This way I believe that you also won&#39;t get issues (although I didn&#39;t try this).


</details>



# 答案3
**得分**: 0

你只需使用 Where 谓词(存在索引为 0 的问题)

```csharp
var result = listofInt.Where((x, i) => i != 0 && x % i != 0).ToList();

i 表示 listOfInt 中项目 x 的索引。

英文:

you just use Where predicate (with the problem of index = 0)

var result = listofInt.Where((x, i) =&gt; i != 0 &amp;&amp; x % i != 0).ToList();

i represents the index of item x in the listOfInt

huangapple
  • 本文由 发表于 2023年5月7日 18:22:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定