gpt4 book ai didi

c# - 如何为 C# 字典中的多个键分配一个值?

转载 作者:行者123 更新时间:2023-12-04 04:31:10 32 4
gpt4 key购买 nike

我想定义一个字母字典。在这本词典中,字符是键,并且为每个字符分配了一个值。
我以最简单的方式编写了它,您可以看到一些键具有相同的值。

var testDict = 
new Dictionary <char, int>() {
{'A', 1}, {'E', 1}, {'I', 1},
{'O', 1}, {'U', 1}, {'L', 1},
{'N', 1}, {'R', 1}, {'S', 1},
{'T', 1},
{'D', 2}, {'G', 2},
{'B', 3}, {'C', 3}, {'M', 3}, {'P', 3},
{'F', 4}, {'H', 4}, {'V', 4}, {'W', 4}, {'Y', 4},
{'K', 5},
{'J', 8}, {'X', 8},
{'Q',10}, {'Z',10}};
我需要在其中找到字母并使用分配的值。我怎样才能以更简洁的方式编写它?

最佳答案

一个 字典 是键和值的集合。在这个集合中,每个键都映射到一个值。它已在 System.Collection.Generics 命名空间中实现。
另一方面,有一个名为 的集合。查找 它表示键和值之间的一对多映射。 IE。它将一个键映射到一个或多个值,并且位于 System.Linq 命名空间中。
您可以转换任何已实现 的集合IEnumerable 接口(interface)和 System.Linq 命名空间中的 ToLookup() 方法来构造 查找 收藏。
阅读更多关于 Lookup收藏和Dictionary在有关 C# 语言的 Microsoft 教程中。
在这个问题中,我们可以考虑一个包含两个字段的包,一个句子中的“Alphabet”字符和它的“Counts”。

class Package
{
public char Alphabet;
public int Counts;
}
然后构造 Lookup 数据结构:
public static void LookupExample()
{
// Create a dictionary of Packages to put into a Lookup data structure.
var testDict = new Dictionary <char, int>() {
new Package { Alphabet = 'A', Counts = 1},
new Package { Alphabet = 'B', Counts = 3},
new Package { Alphabet = 'C', Counts = 3},
new Package { Alphabet = 'D', Counts = 2},
new Package { Alphabet = 'E', Counts = 1},
new Package { Alphabet = 'F', Counts = 4},
new Package { Alphabet = 'G', Counts = 2},
new Package { Alphabet = 'H', Counts = 4},
new Package { Alphabet = 'I', Counts = 1},
new Package { Alphabet = 'J', Counts = 8},
new Package { Alphabet = 'K', Counts = 5},
new Package { Alphabet = 'L', Counts = 1},
new Package { Alphabet = 'M', Counts = 3},
new Package { Alphabet = 'N', Counts = 1},
new Package { Alphabet = 'O', Counts = 1},
new Package { Alphabet = 'P', Counts = 3},
new Package { Alphabet = 'Q', Counts = 10},
new Package { Alphabet = 'R', Counts = 1},
new Package { Alphabet = 'S', Counts = 1},
new Package { Alphabet = 'T', Counts = 1},
new Package { Alphabet = 'U', Counts = 1},
new Package { Alphabet = 'V', Counts = 4},
new Package { Alphabet = 'W', Counts = 4},
new Package { Alphabet = 'X', Counts = 8},
new Package { Alphabet = 'Y', Counts = 4},
new Package { Alphabet = 'Z', Counts = 10}};


// Create a Lookup to organize the packages. Use the Counts of each Alphabet as the key value.
// Select Alpahbet appended to each Counts in the Lookup.
Lookup<int, char> lookup = (Lookup<int, char>)testDict.ToLookup(p => p.Counts, p => p.Alphabet);

// Iterate through each IGrouping in the Lookup and output the contents.
foreach (IGrouping<int, char> packageGroup in lookup)
{
// Print the key value of the IGrouping.
Console.WriteLine(packageGroup.Key);
// Iterate through each value in the IGrouping and print its value.
foreach (char chr in packageGroup)
Console.WriteLine(" {0}", Convert.ToString(chr));
}
}
这有助于将“计数”视为新键并为其分配多个“字母”字符,它们的“计数”值具有相同的数量!

关于c# - 如何为 C# 字典中的多个键分配一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66117023/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com