Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何删除列表中的每个N项,直到列表计数超过目标值?_C#_List_Linq_For Loop - Fatal编程技术网

C# 如何删除列表中的每个N项,直到列表计数超过目标值?

C# 如何删除列表中的每个N项,直到列表计数超过目标值?,c#,list,linq,for-loop,C#,List,Linq,For Loop,这里有一张短名单。它的价值并不重要,比如: List<short> resultTemp = new List<short>{1,2,3,4,5,6,7,8,9...}; 此代码应该通过从结果列表中删除第n项来减少结果列表计数 例1: List<short>{1,2,3,4,5,6,7,8,9,10}.Count == 10; var targetItemsCount = 5; result应该是{1,3,5,7,9},result.Count应该是==5

这里有一张短名单。它的价值并不重要,比如:

List<short> resultTemp = new List<short>{1,2,3,4,5,6,7,8,9...};
此代码应该通过从结果列表中删除第n项来减少结果列表计数

例1:

List<short>{1,2,3,4,5,6,7,8,9,10}.Count == 10;
var targetItemsCount = 5;
result应该是{1,3,5,7,9},result.Count应该是==5

例2:

List<short>{1,2,3,4,5,6,7,8,9}.Count == 9;
var targetItemsCo:nt = 3;
result应该是{1,4,7},result.Count应该是==3

但它应该停止删除它,在代码中的某个地方使结果计数等于targetItemScont 42,但它的值在其他地方并不重要。 代码是:

var currentItemsCount = resultTemp.Count;

var result = new List<short>();

var targetItemsCount = 42;
var counter = 0;
var counterResettable = 0;

if (targetItemsCount < currentItemsCount)
{
    var reduceIndex = (double)currentItemsCount / targetItemsCount;

    foreach (var item in resultTemp)
    {
        if (counterResettable < reduceIndex || 
            result.Count + 1 == currentItemsCount - counter)
        {
            result.Add(item);
            counterResettable++;
        }
        else
        {
            counterResettable = 0;
        }
        counter++;
    }
}
本例中的result.Count等于41,但应为==targetItemScont==42


我如何删除列表中的每个N项直到列表结束。如果我的理解是正确的,则使用C计算比目标值更多的数值?

public static void run()
{
    var inputs =
        new List<Input>{
          new Input{ 
              Value = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },`
              TargetCount = 5, ExpectedOutput= new List<int>{1,3,5,7,9} 
          },
          new Input{  
              Value = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 },
              TargetCount = 3, ExpectedOutput= new List<int>{1,4,7} 
          },
        };

    foreach (var testInput in inputs)
    {
        Console.WriteLine($"# Input = [{string.Join(", ", testInput.Value)}]");
        var result = Reduce(testInput.Value, testInput.TargetCount);
        Console.WriteLine($"# Computed Result = [{string.Join(", ", result)} ]\n");
    }
}

static List<int> Reduce(List<int> input, int targetItemsCount)
{
    while (input.Count() > targetItemsCount)
    {
        var nIndex = input.Count() / targetItemsCount;
        input = input.Where((x, i) => i % nIndex == 0).ToList();
    }
    return input;
}

class Input
{
    public List<int> ExpectedOutput;
    public List<int> Value;
    public int TargetCount;
}
结果:

输入=[1,2,3,4,5,6,7,8,9,10] 计算结果=[1,3,5,7,9]

输入=[1,2,3,4,5,6,7,8,9] 计算结果=[1,4,7]

尝试一下:

var resultTemp = Enumerable.Range(1, 9).ToList();
var targetItemsCount = 3;

var roundingError = resultTemp.Count % targetItemsCount;
var reduceIndex   = (resultTemp.Count - roundingError) / targetItemsCount;

List<int> result;
if (reduceIndex <= 1)
    result = resultTemp.Take(targetItemsCount).ToList();
else
    result = resultTemp.Where((a, index) => index % reduceIndex == 0).Take(targetItemsCount).ToList();
用你给出的例子试过了,也给了42个旋转,列表是1到100,它将每2个项目删除一次,直到它达到42,所以列表中的最后一个项目将是83


正如我所说的,尝试一下,让我知道它是否符合您的要求。

为了保证您获得预期数量的选定项目:

double increment = Convert.ToDouble(resultTemp.Count) / targetItemsCount;

List<short> result = Enumerable.Range(0, targetItemsCount).
                                Select(x => resultTemp[(int)(x * increment)]).
                                ToList();
请注意,在以下情况下

List<short>{1,2,3,4,5,6,7,8,9}.Count == 9;
var targetItemsCount = 6;
结果将是[1,2,4,5,7,8],即在需要时向下舍入索引

此外,还需要添加验证targetItemScont>0,targetItemScont
你为什么要这样做?当您通过它进行调试时,为什么它停止在41而不是42?那么N是targetItemScont?与其给出3个可能的输入和各自的输出,不如解释一下算法,第一个步骤是什么?如果TargetItemScont大于源列表中的元素数怎么办?@XDTTTransform有2个ALG,我应该组合1个,以便从列表中删除元素,以防结果列表计数超过TargetItemScont。2我可以只删除第一个或最后一个第n个项目,但当我删除每个第n个元素时,结果会更平滑。@schlonzo它应该不会删除任何内容,如果会被跳过。你怎么敢放。在行尾,而不是在下一行的开始处。@RandRandom所以sue meLike你的解决方案,它修复了我的一个问题,但那是错的。-这只是一个禁忌,我永远不会投票给一个这样做的人。愤怒@随机检查你的解决方案与74的解决方案相比,在{1,2,3,4,5,6,7,8,9}和targetItemScont=5上的处理效果更好。这是输出1 2 4 6 8,而你的得到1 2 3 4 5。这并不意味着,你的坏或不工作,它工作,但vc 74更好一点。@U_blond先生-这就是我在评论中所说的错误-我完全推荐他的解决方案而不是我的,接受它,但不要复制他的坏位置。忘了你曾经看到过吗