【问题标题】:Alter a list of strings based on the items根据项目更改字符串列表
【发布时间】:2015-09-10 05:56:09
【问题描述】:

在将其输出回客户端之前,我想更改的列表有问题。

为了这个问题,我将发布一个列表示例以及我需要如何查看结果,因为我已经查看了 Intersect,Except 以及我能想到的所有其他内容,但没有得到我的结果正在寻找。

示例列表:

1、4、6、8
1、2、6、8
2、4、6、8
3、4、5、7

所需结果:

1, 4, 6, 8 //初始行
-, 2, -, - //未更改的项目将显示为 -
2, 4, -, -
3、-、5、7

我真的希望我解释得很好。

如果需要,我很乐意进一步解释。

提前感谢您的建议,到目前为止,我已经为此绞尽脑汁。 ;)

我尝试的内容太多,无法在这里输入,所以这是我目前所拥有的。除了根本不会对数据做任何事情,因为它认为行不同,所以它们保持不变。

private List<List<string>> FilterData(List<string[]> datatable)
    {
        List<string> previousRow = new List<string>();
        List<string> currentRow = new List<string>();
        List<string> rowDifferences = new List<string>();

        List<List<string>> resultingDataset = new List<List<string>>();

        foreach (var item in datatable)
        {
            if (previousRow == null)
            {
                previousRow = item.ToList();
                continue;
            }

            currentRow = item.ToList();

            rowDifferences = currentRow.Except(previousRow).ToList();
            resultingDataset.Add(rowDifferences);
        }
        return resultingDataset;
    }

【问题讨论】:

  • 你尝试过什么吗?你写了一些代码吗?
  • StackOverflow 是一个您可以就自己编写的代码获得帮助的地方,因此您需要先尝试一下并发布您想出的内容以获得积极的响应。跨度>
  • 它们是 4 个不同的列表吗?
  • 这是一个字符串列表。
  • 嗯,这是我要解决的第一件事。每个条目都有多个项目,因此值得在您的数据结构中反映这一点。您不想将每个元素解析为列表,然后多次重新格式化。看来您可能想要一个 List&lt;int?[]&gt;List&lt;List&lt;int?&gt;&gt;,使用 nullint? 表示“未更改”。

标签: c# list list-manipulation


【解决方案1】:

您必须在代码中更改一些内容;

代码如下:

 private List<string[]> FilterData(List<string[]> datatable)
    {
        // List is made of String Array, so need string[] variable not list
        string[] previousRow = null ;
        string[] currentRow;
        string[] rowDifferences ;

        // to store the result
        List<string[]> resultingDataset = new List<string[]>();

        foreach (var item in datatable)
        {
            if (previousRow == null)
            {
                previousRow = item;
                resultingDataset.Add(previousRow); // add first item to list
                continue;
            }

            currentRow = item; 

            // check and replace with "-" if elment exist in previous 
            rowDifferences = currentRow.Select((x, i) => currentRow[i] == previousRow[i] ? "-" : currentRow[i]).ToArray();  
            resultingDataset.Add(rowDifferences);

            // make current as previos
            previousRow = item;
        }
        return resultingDataset;
    }

检查这个dotnetfiddle

【讨论】:

  • 我知道我很接近了!看起来这与我所拥有的最接近。我会检查所有不同的答案,看看哪个最适合我。到目前为止,这看起来像是那个候选人!
  • 确实如此!这是最接近我想要的方式。没有冒犯其他人,因为他们也有我想要的变异结果,但这段代码坚持我所拥有的。我喜欢这样。
【解决方案2】:
private static List<List<string>> FilterData(List<List<string>> datatable)
{
    var result = new List<List<string>>();

    for(var rowindex = 0; rowindex < datatable.Count; rowindex++)
    {
        // Clone the string list
        var refrow = datatable[rowindex]
            .Select(item => (string)item.Clone()).ToList(); 

        result.Add(refrow);

        // First row will not get modify anyway
        if (rowindex == 0) continue;

        var row = result[rowindex];
        // previous row of result has changed to "-", so use the original row to compare
        var prevrow = datatable[rowindex - 1]; 
        for(var columnindex = 0; columnindex < row.Count; columnindex++)
        {
            if (row[columnindex] == prevrow[columnindex])
                row[columnindex] = "-";
        }
    }

    return result;
}

fiddle

【讨论】:

    【解决方案3】:
    public static List<List<T>> RemoveDuplicates<T>(this List<List<T>> items, T replacedValue) where T: class
    {
        List<List<T>> ret = items;
    
        items.ForEach(m=> {
            var ind = items.IndexOf(m);
    
            if(ind==0)
            {
                ret.Add(items.FirstOrDefault());
            }
            else
            {
                var prevItem = items.Skip(items.IndexOf(m)-1).FirstOrDefault();
    
                var item = new List<T>();
                for(var a = 0; a < prevItem.Count; a++)
                {
                    item.Add(prevItem[a] == m[a]? replacedValue : m[a]);
                }
    
                ret.Add(item);
            }
        });
    
        return ret;
    }
    

    使用方法:

    var items = new List<List<string>>{
        new List<string>{ "1", "4", "6", "8" },
        new List<string>{ "1", "2", "6", "8" },
        new List<string>{ "2", "4", "6", "8" },
        new List<string>{ "3", "4", "5", "7" }
    };
    
    var result = items.RemoveDuplicates("-");
    

    dotNetFiddle:https://dotnetfiddle.net/n36p64

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多