如何在带有某些条件的linq查询中分配两个列表

本文关键字:分配 列表 两个 查询 linq 条件 | 更新日期: 2023-09-27 18:04:06

我有两个列表,我这样分配其中一个:

var query = Enumerable.Range(0, 1440).Select((n, index) =>
{
    if ((index >= 525 && index <= 544) || (index >= 600 && index <= 749) || (index >= 810 && index <= 1079) || (index >= 1300 && index <= 1439))
        return 0;
    else if (index >= 1080 && index <= 1299)
        return 1;
    else if (index >= 545 && index <= 599)
        return 3;
    else if (index >= 750 && index <= 809)
        return 4;
    else
        return 2;
}).ToList();

第二个列表名为lst2。我想根据我的第一个列表查询将它赋值为"0"或"1"。因此,如果query为"1"或"2",则lst2的相同索引和先前为"0"值的索引应该为"1"。如果查询列表为"3"或"4",则lst2中相同的索引和前一个值为"1"的索引应该为"0"。此外,如果查询的第一个索引值为"3"或"4",那么lst2的相同索引值应该为"0"。例如,

query = {3,3,3,0,0,0,2,2,0,0,0,0,4,4,4,4,0,0,0,0,1,1,1,0,0,2,2,0,0,4,4}
lst2  = {0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0}

我该怎么做?

EDIT:如果查询的任意索引值为3或4,则lst2的相同索引值必须为0。如果查询的任何索引值为3或4,且前一个索引值为0,则lst2的相同索引值必须为0。同样地;如果query的任意索引值为1或2,则lst2的相同索引值必须为1。

如何在带有某些条件的linq查询中分配两个列表

如果查询的任意一个索引值为1或2且前一个索引值为0,则lst2的相同索引值必须为1

另一种方法,只在需要时填充跟踪0。性能方面,最好还是反向循环,但我刚刚看到T_D已经这样做了,所以就向前看->向后看而言,下面的基本相同,但使用了其他语法和不同的末尾0填充符。

    var arr = new int[query.Count];
    int cnt = query.Count - 1, toappendindex = -1;
    Func<int,int,int> getval = (ind, val) =>
    {
        if (val == 3 || val == 4) return  0;
        if (val == 2 || val == 1) return  1;
        if (ind == cnt) return -1;
        return arr[ind + 1];
    };   
    for (int ind = cnt; ind >= 0; ind-- ) 
    {
        if ((arr[ind] = getval(ind,query[ind])) == -1)
            toappendindex = ind; //only if there are trailing 0's
    }
    if (toappendindex > 0) 
        for (; toappendindex < arr.Length; toappendindex++) arr[toappendindex] = arr[toappendindex - 1];
    //var lst2 = arr.ToList(); if list is needed instead of array, otherwise arr could be used directly

试一试。

        List<int> query = Enumerable.Range(0, 1440).Select((n, index) =>
        {
            if ((index >= 525 && index <= 544) || (index >= 600 && index <= 749) || (index >= 810 && index <= 1079) || (index >= 1300 && index <= 1439))
                return 0;
            else if (index >= 1080 && index <= 1299)
                return 1;
            else if (index >= 545 && index <= 599)
                return 3;
            else if (index >= 750 && index <= 809)
                return 4;
            else
                return 2;
        }).ToList();
        Console.WriteLine(string.Concat("{", string.Join(",", query.ToArray()), "}"));
        List<int> lst2 = Enumerable.Range(0, 1440).Select((n, index) =>
        {
            if (query[index] == 1 || query[index] == 2)
                return 1;
            else if (query[index] == 3 || query[index] == 4)
                return 0;
            else
            {
                int retval = 1;
                //look ahead
                for (int i = index; i < query.Count; i++)
                {
                    if (query[i] == 1 || query[i] == 2)
                    {
                        break;
                    }
                    if (query[i] == 3 || query[i] == 4)
                    {
                        retval = 0;
                        break;
                    }
                }
                return retval;
            }
        }).ToList();
        Console.WriteLine(string.Concat("{", string.Join(",", lst2.ToArray()), "}"));

让我知道,如果这是你正在寻找什么。如果愿意,可以用var替换List。我只是希望它是强类型的,这样我可以很容易地检查输出。

我希望这是你需要的:

    int n = 1440;
    byte[] query = new byte[n];
    byte[] lst2 = new byte[n];
    byte mode = 0;
    bool first = true;
    for(int index = n-1; index >= 0; index--)
    {
        if ((index >= 525 && index <= 544) || (index >= 600 && index <= 749) || (index >= 810 && index <= 1079) || (index >= 1300 && index <= 1439))
            query[index] = 0;
        else if (index >= 1080 && index <= 1299)
            query[index] = 1;
        else if (index >= 545 && index <= 599)
            query[index] = 3;
        else if (index >= 750 && index <= 809)
            query[index] = 4;
        else
            query[index] = 2;
        if(query[index] == 3 || query[index] == 4)
        {
            mode = 0;
            lst2[index] = 0;
        }
        else if(query[index] == 1 || query[index] == 2)
        {
            if(first)
            {
                //change ending zeros to 1
                for(int j=index+1; j < n; j++)
                    lst2[j] = 1;
                first = false;
            }
            mode = 1;
            lst2[index] = 1;
        }
        else
        {   
            lst2[index] = mode;
        }
    }