如何处理将重复的int输入到Int,String的2D SortedList中

时间:2015-01-28 17:31:35

标签: c# list error-handling sortedlist

我之前没有使用过SortedLists的经验并且遇到了错误"具有相同密钥的条目已经存在"由于试图在列表中输入重复的Ints。这是对我的疏忽,认为这不会发生(这很愚蠢)。

列表如下所示:

("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]);

排序列表包含段落的句子和程序计算的句子分数。然后我需要得分最高的句子,但不知道处理这个错误。

我很好,两个句子都是&#34; top&#34;并且两者都以某种方式输出,或者任何一个被选为顶部,除非有一个已经更高。

2 个答案:

答案 0 :(得分:1)

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";
      }
 }

答案 1 :(得分:1)

您可以创建ScoredSentence类,例如

,而不是使用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;
    }
}

然后你可以将它全部存储在List中,例如

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";

    }