【发布时间】:2019-06-13 07:21:06
【问题描述】:
我正在尝试按如下方式更改我的字典中 Keys 的值:
//This part loads the data in the iterator
List<Recommendations> iterator = LoadBooks().ToList();
//This part adds the data to a list
List<Recommendations> list = new List<Recommendations>();
foreach (var item in iterator.Take(100))
{
list.Add(item);
}
//This part adds Key and List as key pair value to the Dictionary
if (!SuggestedDictionary.ContainsKey(bkName))
{
SuggestedDictionary.Add(bkName, list);
}
//This part loops over the dictionary contents
for (int i = 0; i < 10; i++)
{
foreach (var entry in SuggestedDictionary)
{
rec.Add(new Recommendations() { bookName = entry.Key, Rate = CalculateScore(bkName, entry.Key) });
entry.Key = entry.Value[i];
}
}
但它说“属性或索引器 KeyValuePair>.Key 无法分配给。是只读的。我真正想做的是在这里更改字典 Key 的值并为其分配另一个值。
【问题讨论】:
-
你到底想做什么?一些输入数据和预期输出数据在这里可能会有所帮助。 (请看stackoverflow.com/help/mcve)
-
为什么需要字典?从您的代码看来,您只需要一个列表
-
我正在尝试计算 Pearson 相关性,它需要一个 KeyValuePair 来比较内容。这就是我使用字典的原因。 @dtanabe 请检查编辑后的代码。
-
SuggestedDictionary的类型参数是什么? -
@dtanabe 它是一个 Dictionary
> 类型字典
标签: c# dictionary key setter keyvaluepair