修改字典

发布于 2024-09-30 10:00:06 字数 386 浏览 4 评论 0原文

我有一个 Dictionary 一些示例值是

Key1 Value="1","2","3","4","5"
Key2 Value="7","8"
Key3 Value=null

我希望数组长度是所有值的最大值,在我的例子中,Key1 是 5 所以我可以得到这样的结果:

Key1 Value="1","2","3","4","5"
Key2 Value="7","8","","",""
Key3 Value="","","","",""

所以所有的键都有相同的数组长度= 5,并且之前不存在的值是空值“”。 这怎么能做到呢?

I have a Dictionary<string,string[]>
Some example values are

Key1 Value="1","2","3","4","5"
Key2 Value="7","8"
Key3 Value=null

I want to the array length to be the max of all values which in my case is 5 for Key1
so I can get a result as:

Key1 Value="1","2","3","4","5"
Key2 Value="7","8","","",""
Key3 Value="","","","",""

So all Keys have same array length = 5 and the values which didnt exist before are empty values "".
How can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

意中人 2024-10-07 10:00:06

试试这个

        Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
        dic.Add("k1", new List<string>() { "1", "2", "3", "4", "5" });
        dic.Add("k2", new List<string>() { "7", "8" });
        dic.Add("k3", new List<string>());

        var max = dic.Max(x => x.Value.Count);
        dic.ToDictionary(
            kvp => kvp.Key,
            kvp =>
            {
                if (kvp.Value.Count < max)
                {
                    var cnt = kvp.Value.Count;
                    for (int i = 0; i < max - cnt; i++)
                        kvp.Value.Add("");
                }
                return kvp.Value;
            }).ToList();

Try this

        Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
        dic.Add("k1", new List<string>() { "1", "2", "3", "4", "5" });
        dic.Add("k2", new List<string>() { "7", "8" });
        dic.Add("k3", new List<string>());

        var max = dic.Max(x => x.Value.Count);
        dic.ToDictionary(
            kvp => kvp.Key,
            kvp =>
            {
                if (kvp.Value.Count < max)
                {
                    var cnt = kvp.Value.Count;
                    for (int i = 0; i < max - cnt; i++)
                        kvp.Value.Add("");
                }
                return kvp.Value;
            }).ToList();
凉墨 2024-10-07 10:00:06

我会使用类似的方法将字典封装到我自己的类中,除非每当添加值时,如果必须扩展该数组以包含该值,则需要扩展字典中所有其他数组值的大小。

如果您追求效率,每次发生这种情况时我都会将数组加倍,以避免代码效率低下。您可以跟踪所有数组的虚拟“最大大小”,即使您通过在类 int 变量中跟踪它来有效地将它们加倍。

I'd encapsulate a Dictionary into my own class with similar methods, except whenever a value is added, if you must extend that array to contain the value, you extend the size of all other array values in the dictionary.

If you're going for efficiency, I'd double the arrays each time this happens to avoid inefficient code. You can keep track of the virtual 'max size' of all the arrays even if you're effectively doubling them by keeping track of it in a class int variable.

时光瘦了 2024-10-07 10:00:06
Dictionary<string, string[]> source = GetDictionary();

targetSize = source.Values.Select(x => x.Length).Max();

Dictionary<string, string[]> result = source.ToDictionary(
  kvp => kvp.Key,
  kvp => kvp.Value != null ?
    kvp.Value.Concat(Enumerable.Repeat("", targetSize - kvp.Value.Length)).ToArray() :
    Enumerable.Repeat("", targetSize).ToArray
);
Dictionary<string, string[]> source = GetDictionary();

targetSize = source.Values.Select(x => x.Length).Max();

Dictionary<string, string[]> result = source.ToDictionary(
  kvp => kvp.Key,
  kvp => kvp.Value != null ?
    kvp.Value.Concat(Enumerable.Repeat("", targetSize - kvp.Value.Length)).ToArray() :
    Enumerable.Repeat("", targetSize).ToArray
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文