C# 获取 IEnumerable>来自 Dictionary>;通过笛卡尔积

发布于 2025-01-09 10:09:10 字数 980 浏览 0 评论 0 原文

从这本字典中

var foo = new Dictionary<string, IEnumerable<object>>
        {
            {"key1", new object[] {1}}, 
            {"key2", new[] {"one", "two"}}, 
            {"key3", new[] {"three", "four"}}
        }

我想得到这个 IEnumerable>

IEnumerable<Dictionary<string, object>> bar = {
     {{"key1", 1}, {"key2", "one"}, {"key3", "three"}},
     {{"key1", 1}, {"key2", "one"}, {"key3", "four"}},
     {{"key1", 1}, {"key2", "two"}, {"key3", "three"}},
     {{"key1", 1}, {"key2", "two"}, {"key3", "four"}}
} 

简单地说,我需要 C# 的 python itertools.product 函数的模拟 a 的笛卡尔积列表字典

我已阅读生成所有可能的组合,但不知道我应该如何更改它并且我找不到关于带有字典(带键)结果的笛卡尔积的解决方案。

提前致谢

from this dictionary

var foo = new Dictionary<string, IEnumerable<object>>
        {
            {"key1", new object[] {1}}, 
            {"key2", new[] {"one", "two"}}, 
            {"key3", new[] {"three", "four"}}
        }

i'd like to get this IEnumerable<dictionary<string, object>>

IEnumerable<Dictionary<string, object>> bar = {
     {{"key1", 1}, {"key2", "one"}, {"key3", "three"}},
     {{"key1", 1}, {"key2", "one"}, {"key3", "four"}},
     {{"key1", 1}, {"key2", "two"}, {"key3", "three"}},
     {{"key1", 1}, {"key2", "two"}, {"key3", "four"}}
} 

To speak simply, I need C#'s analog of python itertools.product function Cartesian product of a dictionary of lists.

I've read Generating all Possible Combinations but couldn't figure out how i should change it and i couldn't found solution about cartesian product with dictionaries (with keys) result.

Thanks in advance

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

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

发布评论

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

评论(1

指尖凝香 2025-01-16 10:09:10

快速的解决方案是仅对字典值进行笛卡尔积,同时保留有关键的信息,然后重新创建字典:

           // [[("key1",1)],[("key2","one"),("key2,"two")],[("key3","three"),("key3","four")]]
            var collectionsOfKeyValuePairs =
                foo.Select(k => k.Value.Select(v => new KeyValuePair<string, object>(k.Key, v)));
            // [[(key1,1),(key2,one),(key3,three)],
            //  [(key1,1),(key2,one),(key3,four)],...etc
            var product = CartesianProduct(collectionsOfKeyValuePairs);
            
            var dictionaries = product.Select(collection=> collection.ToDictionary(k=> k.Key, k=> k.Value));

Quick solution would be to do only cartesian product of dictionary values while preserving information about keys, and next recreate dictionaries:

           // [[("key1",1)],[("key2","one"),("key2,"two")],[("key3","three"),("key3","four")]]
            var collectionsOfKeyValuePairs =
                foo.Select(k => k.Value.Select(v => new KeyValuePair<string, object>(k.Key, v)));
            // [[(key1,1),(key2,one),(key3,three)],
            //  [(key1,1),(key2,one),(key3,four)],...etc
            var product = CartesianProduct(collectionsOfKeyValuePairs);
            
            var dictionaries = product.Select(collection=> collection.ToDictionary(k=> k.Key, k=> k.Value));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文