从 C# 中缺少键的列表创建完整集
Create full set from a list with missing keys in C#
我进行了大量的挖掘工作,但似乎找不到这个特定问题的答案;类似问题的答案,但不是这样。
本质上,我要做的是在列表中添加具有默认值的缺失键。我有一个 List>() 结构如下:
键:值
a : Apple
b : Orange
c : Mango
b : Lime
c : Lemon
a : Berry
d : Carrot
从上面的列表中,我想创建一个完整的集合,其中缺少密钥。
预期输出
预期键:值
a : Apple
b : Orange
c : Mango
d : ""
a : ""
b : Lime
c : Lemon
d : ""
a : Berry
b : ""
c : ""
d : Carrot
是否可以使用 C# 以执行方式执行此操作?
给定
var list = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("a", "Apple"),
new KeyValuePair<string, string>("b", "Orange"),
new KeyValuePair<string, string>("c", "Mango"),
new KeyValuePair<string, string>("b", "Lime"),
new KeyValuePair<string, string>("c", "Lemon"),
new KeyValuePair<string, string>("a", "Berry"),
new KeyValuePair<string, string>("d", "Carrot"),
};
通过执行以下操作,我们可以使用 for-loop 轻松完成此操作。下面将获得列表中的不同键。
var distinctKeys = list
.Select(pair => pair.Key)
.Distinct()
.OrderBy(pair => pair)
.ToArray();
如果您想直接对 distinctKeys
进行硬编码并节省计算不同键的时间,您可以执行以下操作:
var distinctKeys = new[] {"a", "b", "c", "d"};
如果需要填充键,下面的循环将在给定索引处插入对。
var lastKeyIndex = -1;
for (var index = 0; index < list.Count; index++)
{
var currentKeyIndex = lastKeyIndex + 1 == distinctKeys.Length ? 0 : lastKeyIndex + 1;
var currentKey = distinctKeys[currentKeyIndex];
if (list[index].Key != currentKey)
{
list.Insert(index, new KeyValuePair<string, string>(currentKey, string.Empty));
}
lastKeyIndex = currentKeyIndex;
}
for (var index = lastKeyIndex; index < distinctKeys.Length; index++)
{
list.Add(new KeyValuePair<string, string>(distinctKeys[index], string.Empty));
}
输出
a : Apple
b : Orange
c : Mango
d :
a :
b : Lime
c : Lemon
d :
a : Berry
b :
c :
d : Carrot
我进行了大量的挖掘工作,但似乎找不到这个特定问题的答案;类似问题的答案,但不是这样。
本质上,我要做的是在列表中添加具有默认值的缺失键。我有一个 List
键:值
a : Apple
b : Orange
c : Mango
b : Lime
c : Lemon
a : Berry
d : Carrot
从上面的列表中,我想创建一个完整的集合,其中缺少密钥。
预期输出
预期键:值
a : Apple
b : Orange
c : Mango
d : ""
a : ""
b : Lime
c : Lemon
d : ""
a : Berry
b : ""
c : ""
d : Carrot
是否可以使用 C# 以执行方式执行此操作?
给定
var list = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("a", "Apple"),
new KeyValuePair<string, string>("b", "Orange"),
new KeyValuePair<string, string>("c", "Mango"),
new KeyValuePair<string, string>("b", "Lime"),
new KeyValuePair<string, string>("c", "Lemon"),
new KeyValuePair<string, string>("a", "Berry"),
new KeyValuePair<string, string>("d", "Carrot"),
};
通过执行以下操作,我们可以使用 for-loop 轻松完成此操作。下面将获得列表中的不同键。
var distinctKeys = list
.Select(pair => pair.Key)
.Distinct()
.OrderBy(pair => pair)
.ToArray();
如果您想直接对 distinctKeys
进行硬编码并节省计算不同键的时间,您可以执行以下操作:
var distinctKeys = new[] {"a", "b", "c", "d"};
如果需要填充键,下面的循环将在给定索引处插入对。
var lastKeyIndex = -1;
for (var index = 0; index < list.Count; index++)
{
var currentKeyIndex = lastKeyIndex + 1 == distinctKeys.Length ? 0 : lastKeyIndex + 1;
var currentKey = distinctKeys[currentKeyIndex];
if (list[index].Key != currentKey)
{
list.Insert(index, new KeyValuePair<string, string>(currentKey, string.Empty));
}
lastKeyIndex = currentKeyIndex;
}
for (var index = lastKeyIndex; index < distinctKeys.Length; index++)
{
list.Add(new KeyValuePair<string, string>(distinctKeys[index], string.Empty));
}
输出
a : Apple
b : Orange
c : Mango
d :
a :
b : Lime
c : Lemon
d :
a : Berry
b :
c :
d : Carrot