2017-02-18 74 views
1

我想統計集合中列表中差異(或相似性)的數量。下面的代碼是for循環,它會產生正確的結果,將每個其他記錄與第一個記錄。 有沒有辦法做得更好?有了Linq,也許?Linq統計集合中列表中的差異數

public void Main(){ 
     _records = new ObservableCollection<Record>(); 
     _records.Add(new Record { Name = "Correct", Results = new List<string> { "A", "B","C" } , Score="100%"}); 
     _records.Add(new Record { Name = "John", Results = new List<string> { "A", "B" ,"C" } }); //Expect score to be 3/3 (100%) 
     _records.Add(new Record { Name = "Joseph", Results = new List<string> { "A", "C","B" } }); //Expect score to be 2/3 (67%) 
     _records.Add(new Record { Name = "James", Results = new List<string> { "C", "C", "C" } }); //Expect score to be 1/3 (33%) 

     for(int i = 1; i < _records.Count(); i++) // Each Results in the _records except "Correct" 
     { 
      float score = _records[0].Results.Count(); 
      _records[i].Score = string.Format("{0:p1}", (score - CountDifferences(_records[i].Results, _records[0].Results))/score); 
     } 


} 

private int CountDifferences(List<string> x, List<string> y) 
{ 
    return (x.Zip(y, (a, b) => a.Equals(b) ? 0 : 1).Sum()); 
} 
+0

爲什麼你期望第三筆記錄的分數是67%? – Enigmativity

+0

我只能假設你真的意思是約瑟得到33%的分數;正如Enigmativity暗示的那樣。因爲按索引排列的唯一等級是A.根據這個假設,我給出了一個使用Dictionarys而不是你的自定義類的答案。 – Edward

回答

-1

我做了LINQ(使用lambda表達式)語句處理使用stude的字典nts'的名稱作爲鍵和自定義類記錄爲值。然後將其與預期成績的單獨列表進行比較。

List<string> expected = new List<string>(){"A", "B","C" }; 
Dictionary<string, Record> _records = new Dictionary<string, Record>(); 
_records["John"] = new Record { Name = "John", Results = new List<string> { "A", "B" ,"C" } } ; 
_records["Joseph"]= new Record { Name = "Joseph", Results = new List<string> { "A", "C","B" } } ; 
_records["James"] = new Record { Name = "James", Results = new List<string> { "C", "C", "C" } } ; 

foreach(var v in _records){ 
    decimal count = v.Value.Results.Where((x,index) => expected[index]==x).Count()/(decimal)expected.Count(); 
    v.Value.Score = String.Format("{0:P0}",count); 
} 

foreach(var v in _records) 
    Console.WriteLine("{0} {1}",v.Value.Name,v.Value.Score); 
+0

感謝您的索引功能! – Jean

+0

@waterling我不知道爲什麼這有一個倒票。除了選擇答案之外,您也可以對答案和評論投票,以增加價值或幫助您理解某些內容。這可以在您自己的問題中找到,或者您在尋找問題答案時找到的任何解決方案。謝謝。 – Edward

+0

我沒有downvote。我接受了! @愛德華 – Jean

1

我會去這樣做是這樣的:

var results = 
    from r0 in _records.Take(1) 
    from r in _records 
    let score = (double)r0.Results.Count() 
    let differences = CountDifferences(r.Results, r0.Results) 
    select new { record = r, score = ((score - differences)/score).ToString("p1") }; 

foreach (var result in results) 
{ 
    result.record.Score = result.score; 
} 

我想,hovever建議你沒有.ScoreRecord屬性的得分纔有效,當你可以比較一個記錄對方。這意味着如果你有三個不同的結果,如果你與另外兩個結果進行比較,分數可能會有所不同。

所以我建議這樣的:

public class Record 
{ 
    public string Name; 
    public List<string> Results; 
    public double GetScore(Record benchmark) 
    { 
     var max = benchmark.Results.Count; 
     var differences = benchmark.Results 
      .Zip(this.Results, (a, b) => a == b) 
      .Where(r => r == false) 
      .Count(); 
     return ((double)max - differences)/max; 
    } 
} 

然後就做這個查詢得到的結果:

var results = 
    from r0 in _records.Take(1) 
    from r in _records 
    select new 
    { 
     record = r, 
     score = r.GetScore(r0).ToString("p1") 
    }; 

這給了我:

results