如何并排显示列表和列表列表

How do I display a list and a list of lists side by side

提问人:Oussama Abdellah 提问时间:1/24/2022 更新时间:1/24/2022 访问量:449

问:

我找到了一个并排显示两个列表的代码,但是一个列表和一个列表列表没有运气

这是两个并排列表的代码

for (var i = 0; i < bncount; i++)
{
  //Console.WriteLine(String.Format("{0,-10} | {1,-10}", hed.ElementAt(i),bin.ElementAt(i)));
  Console.WriteLine(String.Format("{0,-10} | {1,-10}", i< hdcount ? hed[i] : string.Empty, i< bncount ? bin[i] : string.Empty));          
}

但是 string.empty 仅适用于列表,而不是列表列表,并且 ElementAt() 也不起作用 我尝试将 linq 与 foreach 一起使用,但没有成功

HED 是字符串列表,BN 是数字序列列表列表

我的输出如下

foreach(var r in bin) //0010 1110 1111
foreach(var m in hed) //red blue white 

我想要以下输出

red 0010
blue 1110
white 1111

red   blue  white
 0     1      1     
 0     1      1     
 1     1      1
 0     0      1  

关于如何在 c# 或 Linq 中执行此操作的任何想法?我尝试的方法要么导致仅从 hed 重新打印一个值,要么从 bin 重新打印所有 vlaues,要么相反

C# 列表 LINQ 嵌套列表

评论


答:

0赞 fbede 1/24/2022 #1

我建议使用不同的结构来存储数据(考虑 OOP 原则)和以下代码来打印数据:

public class Item
{
    public string Name { get; set; }
    public List<int> Values { get; set; }
}

public void Print(List<Item> items)
{
    foreach (var item in items)
    {
        Console.WriteLine($"{item.Name} {string.Join("", item.Values)}");
    }
}

评论

0赞 Oussama Abdellah 1/24/2022
我已经定义了它们,因为我从文件中读取了它们,这就是为什么我不能考虑不同的结构或方法
0赞 Sonyck 1/24/2022 #2

不确定我是否正确理解了这个问题,我认为扩展包括变量定义在内的代码示例会很有帮助。无论如何,如果我理解正确,这将是我的方法:

var listOfString = new List<string>( )
{
    "red",
    "blue",
    "white"
};

var listOfArrays = new List<int[]>( )
{
    new int[] { 0,0,1,0 },
    new int[] { 0,1,1,1 },
    new int[] { 1,1,1,1 }
};

// Here you could add a condition in case you are not 100% sure your arrays are of same length.
for( var i = 0; i < listOfString.Count; i++ )
{
    var stringItem = listOfString[i];
    var arrayItem = listOfArrays[i];

    Console.WriteLine( $"{stringItem} {string.Join( null, arrayItem )}" );
}

评论

0赞 Oussama Abdellah 1/24/2022
非常感谢它的工作原理,我担心它会给我带来同样的错误,即不输出结果,但 System.Collections.Generic.List ...但输出是我提到的第一个示例。关于第二个,我将尝试看看如何定位它,但如果你有一个想法,我想听听。
0赞 SomeBody 1/24/2022 #3

第一个版本并不难:

string reuslt = string.Join("\n", bin.Zip(hed).Select(x => $"{x.Item1} {x.Item2}"));

使用 zip,我们创建一个可枚举的元组,其中第 n 个元组具有第 n 个元素 bin 和第 n 个元素 hed。您可以将这两个项目连接起来。

第二个版本稍微复杂一些:

result = string.Join("\t",hed) + "\n" + 
       string.Join("\n",Enumerable.Range(0, bin.First().Length)
             .Select(x => string.Join("\t\t", bin.Select(str => str[x]))));

我们只需通过键入 hed 字符串来创建标题行。然后,我们创建一个可枚举的数字,这些数字表示字符串中的索引。可枚举的将是 0、1、2、3。然后,我们获取 bin 列表的每个字符串的索引为 0 的 char,然后是 bin 列表的每个字符串的索引为 1 的 char,依此类推。

在线演示:https://dotnetfiddle.net/eBQ54N

评论

0赞 Oussama Abdellah 1/24/2022
实际上,我一分钟前才发现zip,当我实现它时,二进制结果是使用此代码输出System.Collections.Generic.List'1[System.String] var resultsequence = hed。Zip(bin, (第一,第二) => 第一个 + “ - ” + 第二个);当我也尝试实现你的时,我在 Zip 方法上遇到了问题,直到删除了 Select 部分并将其替换为 second) => 第一个 + “ - ” + 第二个它删除了错误,但仍然输出相同的
0赞 afshinbr 1/24/2022 #4

您可以使用字典:

        var hed = new List<string>(){"red", "blue", "white"};
        var bin = new List<string>(){ "0010", "1110", "1111" };
        Dictionary<string, string> Dic = new Dictionary<string, string>();
        for (int i = 0; i < hed.Count; i++)
        {
            Dic.Add(hed[i],bin[i]);
        }

        foreach (var item in Dic)
        {
            Console.WriteLine(item.Key+" "+item.Value);
        }

        Console.ReadKey();

评论

0赞 Oussama Abdellah 1/25/2022
HED 和 BIN 变量已经定义,并且 BIN 被定义为列表列表,因此使用您的方法需要将 BIN 转换为单个列表,这是不可行的