[英]How to assign one value to multiple keys in a C# dictionary?
我想定義一個字母字典。 在這本詞典中,字符是鍵,並且為每個字符分配了一個值。 我以最簡單的方式編寫了它,您可以看到一些鍵具有相同的值。
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}};
我需要在其中找到字母並使用分配的值。 我怎樣才能以更簡潔的方式編寫它?
Dictionary<TKey, TValue>是鍵和值的集合。 在這個集合中,每個鍵都映射到一個值。 它已在 System.Collection.Generics 命名空間中實現。
另一方面,有一個名為Lookup<TKey, TElement>的集合,它表示鍵和值之間的一對多映射。 即它將一個鍵映射到一個或多個值,它位於 System.Linq 命名空間中。
您可以轉換任何在 System.Linq 命名空間中實現了IEnumerable接口和 ToLookup() 方法的 collections 以構造Lookup<TKey, TElement>集合。
在有關 C# 語言的 Microsoft 教程中閱讀有關查找集合和字典的更多信息。
在這個問題中,我們可以考慮一個 package,它由兩個字段組成,一個句子中的“字母”字符和它的“計數”。
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));
}
}
這有助於將“計數”視為新鍵並為其分配多個“字母”字符,它們的“計數”值具有相同的數量!
使用此擴展方法:
public static void Add<TKey, TValue>(this Dictionary<TKey, TValue> dict, TValue value, params TKey[] keys)
{
foreach (var key in keys)
dict.Add(key, value);
}
您可以像這樣創建字典:
var testDict =
new Dictionary<char, int>() {
{1, 'A', 'E', 'I'},
{2, 'D', 'G'},
{3, 'B', 'C', 'M', 'P'},
};
您可以為鍵創建值字典。 然后,您可以使用 linq 將其轉換為字典:
var tempDict = new Dictionary<int, List<char>>
{
{1, new List<char> {'A', 'E','I'}},
{2, new List<char> {'D','G'}}
};
var finalDict = new Dictionary<char, int>();
tempDict.ForEach(x => x.Value.ForEach(y => finalDict[y] = x.Key));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.