合并列表和“合并”排序
我有一个列表列表,我正在尝试弄清楚如何将它们合并到一个独特的列表中,但我还想考虑当前每个列表的排序。
例如:
List<List<string>> ListofLists = new List<List<string>>();
ListofLists.Add(new List<string>() { "Color", "Size", "Dimensions" });
ListofLists.Add(new List<string>() { "Color", "Size", "Dimensions" });
ListofLists.Add(new List<string>() { "Color", "Size", "Weight", "Dimensions" });
我希望“合并”列表的内容为:
颜色、尺寸、重量、尺寸
我看过联合,但这将“重量”添加到列表的末尾 - 我希望它“合并”列表,保持“额外”值内联(尽量)。
我还发现这个thread 很接近,但似乎在随机点插入“额外”值。
抱歉,如果没有很好地解释这一点,我真的不知道如何描述这一点。
编辑:这样做的目的是将多个产品放在一个表格上,并在下面列出它们的规格,在同一行上显示相似的规格。有些产品可能有不同的规格,但大多数情况下它们是相同的。
因此,我试图生成一个可能的规范名称列表,以显示在第一列中。通常,最后的规格与保修相关,顶部的规格与型号等相关。因此,如果产品 3 有一些奇怪的规格,我宁愿将其内联插入某处,而不是在保修期内将其固定在末尾。
示例输出:
Model # | 123 | 456 | 789
Color | red | red | blue
Size | big | big | small
Weight | | | 3
Dimensions | 1" | 1" | 1"x1"x1"
Warranty | 1 | 2 | 3
可能不是最好的示例,但例如 123 和 456 可能没有重量规格。因此,我不想将权重放在底部(当我进行联合时会发生这种情况),我希望它尽可能接近与原始列表的“内联”。在此示例中您看不到,但每个产品可能有 30 个规格,因此最好将重要的规格放在顶部,如其规格列表所反映的那样。
I have a List of List's that I'm trying to figure out how to merge them into one unqiue list, except I also want to take into account the sorting of each list currently.
Ex:
List<List<string>> ListofLists = new List<List<string>>();
ListofLists.Add(new List<string>() { "Color", "Size", "Dimensions" });
ListofLists.Add(new List<string>() { "Color", "Size", "Dimensions" });
ListofLists.Add(new List<string>() { "Color", "Size", "Weight", "Dimensions" });
I want the contents of the "merged" list to be:
Color, Size, Weight, Dimensions
I've looked Unions but this adds "Weight" to the end of the list -- I want it to "merge" the lists keeping the "extra" values inline (as much as possible).
I also found this thread which is close but it seems to insert the 'extra' value at a random point.
Sorry if this isn't explained well, I don't really know how to describe this.
Edit: The goal of this is to place multiple products on a single table and list their specs beneath, showing similar specs on the same row. Some products might have different specs but for the most part they are the same.
So I'm trying to generate a single list of the possible Spec names to show in the first column. Typically the specs at the end are related to warranty, at the top related to models, etc. So if Product 3 has some weird spec I would rather have it inserted inline somewhere than tacked on the end under warranty.
Example output:
Model # | 123 | 456 | 789
Color | red | red | blue
Size | big | big | small
Weight | | | 3
Dimensions | 1" | 1" | 1"x1"x1"
Warranty | 1 | 2 | 3
Probably not the best example, but for instance maybe 123 and 456 don't have a Weight spec. So rather than tack Weight on at the bottom (which is happening when I do a Union) I'd like it to be as close to "inline" with it's original list as possible. You can't see in this example but each product might have 30 specs, so it's desirable to have the important ones on top as their List of Specs reflects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建列表中每个元素的排序图表。 (因此,对于 ACDEFGHJ、ABDFGHIJ、ABCDEGIJ,您将得到 A->C、A->B、C->D、B->D、B->C 等)希望只有一个节点具有没有向内的弧(在本例中为 A);将此元素添加到结果列表中。删除该节点及其向外的弧线,然后重复该过程。如果您有多个没有向内弧的节点,那么您可以随机选择一个。如果没有没有向内弧的节点,那么就有一个循环。你可以通过随机选择一个成员来打破循环,但你必须首先找到循环......
Create a graph of the ordering of each of the elements in the lists. (So for ACDEFGHJ, ABDFGHIJ, ABCDEGIJ you would get A->C, A->B, C->D, B->D, B->C, etc.) Hopefully only one node will have no inward arcs (in this case A); add this element to your resulting list. Remove that node and its outward arcs, and repeat the process. If you have multiple nodes with no inward arcs, then you can choose one at random. If you have no nodes with no inward arcs, then you have a cycle. You could break the cycle by choosing a member at random, but you would have to find the cycle first...
看一下 LINQ Concat 和 Distinct 扩展方法。他们应该能够帮助您完成您所追求的目标。
Take a look at the LINQ Concat and Distinct extension methods. They should be able to help you accomplish what you're after.