如何处理将重复的 int 输入到 Int、String 的 2D SortedList 中
How to handle having a duplicate int entered into a 2D SortedList of Int, String
我以前没有使用 SortedLists 的经验,并且由于尝试在列表中输入重复的整数而遇到错误 "An entry with the same key already exists"。这是我的疏忽,认为这不会发生(这很愚蠢)。
列表如下所示:
("503", "This is a sentence")
("364", "Oh and another one")
("329", "Yes sentences woo!")
("136", "You gets the point")
抛出错误的函数是:
protected void buildSummary()
{
scoreCoord2 = -1;
for (int x1 = 0; x1 < results.Length; x1++)
{
SortedList<int, string> paragraphScoreslist = new SortedList<int, string>();
for (int x2 = 0; x2 < results[x1].Length; x2++)
{
scoreCoord2++;
paragraphScoreslist.Add(intersectionSentenceScores[scoreCoord2], results[x1][x2]);
}
var maxValue = paragraphScoreslist.Max(k => k.Key);
string topSentence = string.Empty;
if (paragraphScoreslist.TryGetValue(maxValue, out topSentence))
{
TextboxSummary.Text += topSentence + "\n";
}
}
}
它中断的具体行是:
paragraphScoreslist.Add(intersectionSentenceScores[scoreCoord2], results[x1][x2]);
排序后的列表包含一段句子和程序计算的句子分数。然后我需要得分最高的句子,但不知道如何处理这个错误。
我觉得这两个句子都是 "top" 并且都以某种方式输出,或者其中一个被选为顶部,除非已经有更高的句子。
SortedList<int, List<string>> paragraphScoreslist = new SortedList<int, List<string>>();
for (int x2 = 0; x2 < results[x1].Length; x2++)
{
scoreCoord2++;
if(paragraphScoreslist.ContainsKey(intersectionSentenceScores[scoreCoord2])
{
paragraphScoreslist[intersectionSentenceScores[scoreCoord2]].Add(results[x1][x2]);
}
else
{
paragraphScoreslist.Add(intersectionSentenceScores[scoreCoord2], new List<string>{results[x1][x2]});
}
}
然后获取流行音乐的顶部:
List<string> topSentences;
if (paragraphScoreslist.TryGetValue(maxValue, out topSentences))
{
foreach(string topSentence in topSentences)
{
TextboxSummary.Text += topSentence + "\n";
}
}
您可以创建一个 ScoredSentence class,例如
,而不是使用 SortedList
public class ScoredSentence
{
public string sentence { get; set; }
public int score { get; set; }
public ScoredSentence(string sentence, int score)
{
this.sentence = sentence;
this.score = score;
}
}
然后你可以把它全部存储在一个列表中,比如
var s1 = new ScoredSentence("this is a sentence", 2);
var s2 = new ScoredSentence("hey there buddy", 4);
var s3 = new ScoredSentence("This is bad", 0);
var scores = new List<ScoredSentence> {s1,s2,s3};
然后你可以用
拉出最高分
int max = scores.Max(s => s.score);
或找到得分最高的句子
var maxScoredSentence = scores.First(s => s.score == max);
这是您的代码中的样子
for (int x1 = 0; x1 < results.Length; x1++)
{
List<ScoredSentence> scoreslist = new List<ScoredSentence>();
for (int x2 = 0; x2 < results[x1].Length; x2++)
{
scoreCoord2++;
scoreslist.Add(new ScoredSentence(results[x1][x2],intersectionSentenceScores[scoreCoord2]));
}
var maxValue = scoreslist.Max(s => s.score);
string topSentence = string.Empty;
TextboxSummary.Text += scoreslist.First(s => s.score == maxValue).sentence + "\n";
}
我以前没有使用 SortedLists 的经验,并且由于尝试在列表中输入重复的整数而遇到错误 "An entry with the same key already exists"。这是我的疏忽,认为这不会发生(这很愚蠢)。
列表如下所示:
("503", "This is a sentence")
("364", "Oh and another one")
("329", "Yes sentences woo!")
("136", "You gets the point")
抛出错误的函数是:
protected void buildSummary()
{
scoreCoord2 = -1;
for (int x1 = 0; x1 < results.Length; x1++)
{
SortedList<int, string> paragraphScoreslist = new SortedList<int, string>();
for (int x2 = 0; x2 < results[x1].Length; x2++)
{
scoreCoord2++;
paragraphScoreslist.Add(intersectionSentenceScores[scoreCoord2], results[x1][x2]);
}
var maxValue = paragraphScoreslist.Max(k => k.Key);
string topSentence = string.Empty;
if (paragraphScoreslist.TryGetValue(maxValue, out topSentence))
{
TextboxSummary.Text += topSentence + "\n";
}
}
}
它中断的具体行是:
paragraphScoreslist.Add(intersectionSentenceScores[scoreCoord2], results[x1][x2]);
排序后的列表包含一段句子和程序计算的句子分数。然后我需要得分最高的句子,但不知道如何处理这个错误。
我觉得这两个句子都是 "top" 并且都以某种方式输出,或者其中一个被选为顶部,除非已经有更高的句子。
SortedList<int, List<string>> paragraphScoreslist = new SortedList<int, List<string>>();
for (int x2 = 0; x2 < results[x1].Length; x2++)
{
scoreCoord2++;
if(paragraphScoreslist.ContainsKey(intersectionSentenceScores[scoreCoord2])
{
paragraphScoreslist[intersectionSentenceScores[scoreCoord2]].Add(results[x1][x2]);
}
else
{
paragraphScoreslist.Add(intersectionSentenceScores[scoreCoord2], new List<string>{results[x1][x2]});
}
}
然后获取流行音乐的顶部:
List<string> topSentences;
if (paragraphScoreslist.TryGetValue(maxValue, out topSentences))
{
foreach(string topSentence in topSentences)
{
TextboxSummary.Text += topSentence + "\n";
}
}
您可以创建一个 ScoredSentence class,例如
,而不是使用 SortedListpublic class ScoredSentence
{
public string sentence { get; set; }
public int score { get; set; }
public ScoredSentence(string sentence, int score)
{
this.sentence = sentence;
this.score = score;
}
}
然后你可以把它全部存储在一个列表中,比如
var s1 = new ScoredSentence("this is a sentence", 2);
var s2 = new ScoredSentence("hey there buddy", 4);
var s3 = new ScoredSentence("This is bad", 0);
var scores = new List<ScoredSentence> {s1,s2,s3};
然后你可以用
拉出最高分int max = scores.Max(s => s.score);
或找到得分最高的句子
var maxScoredSentence = scores.First(s => s.score == max);
这是您的代码中的样子
for (int x1 = 0; x1 < results.Length; x1++)
{
List<ScoredSentence> scoreslist = new List<ScoredSentence>();
for (int x2 = 0; x2 < results[x1].Length; x2++)
{
scoreCoord2++;
scoreslist.Add(new ScoredSentence(results[x1][x2],intersectionSentenceScores[scoreCoord2]));
}
var maxValue = scoreslist.Max(s => s.score);
string topSentence = string.Empty;
TextboxSummary.Text += scoreslist.First(s => s.score == maxValue).sentence + "\n";
}