所以我有這個代碼...這是讓我瘋狂。結果不一致,它取決於列表中對象的順序。c#嵌套for循環檢查比較列表中的每個元素返回奇數結果
基本上覆制了一個對象列表,使用compareMembers()(相對於它100%沒有問題)檢查列表中的每個對象,然後如果它們具有比allowed(num)更多的共同點,那麼它們「相似」我們決定刪除哪一個。下一個具有較低分數的人從temp列表中刪除,並在所有迭代後返回temp列表。
例如,如果我排序使用去除類似的現有的列表:
group.Sort((a, b) => -1 * a.GroupScore().CompareTo(b.GroupScore()));
返回的列表總是中有較少的元素,比當我比較相似,並且該列表是完全隨機的。我知道這個問題在於這一部分......希望它的內容簡單,我只是盯着它太久了。
下面是給出相互衝突的結果的代碼。
public List<MyGroup> RemoveSimilar(List<MyGroup> group, int num)
{
List<MyGroup> temp = group.ToList();
for(int i = 0; i < group.Count - 1;i++)
{
for (int j = i + 1; j < group.Count;j++)
{
if (group[i].compareMembers(group[j]) > num)
{
if (group[i].score < group[j].score)
{
temp.Remove(group[i]);
break;//removed this one might as well stop checking it
}
else
{
temp.Remove(group[j]);
}
}
}
}
return temp;
}
這裏是MyGroup的類:
public class MyGroup
{
public Member firstMember;
public Member secondMember;
public Member thirdMember;
public double score;
public MyGroup()
{
}
public int compareMembers(MyGroup x)
{
int i = 0;
if (this.firstMember == x.firstMember)
{ i++; }
if (this.secondMember == x.secondMember)
{ i++; }
if (this.thirdMember == x.thirdMember)
{ i++; }
if (this.firstMember == x.secondMember)
{ i++; }
if (this.firstMember == x.thirdMember)
{ i++; }
if (this.secondMember == x.thirdMember)
{ i++; }
return i;
}
public MyGroup(Member one, Member two, Member three)
{
firstMember = one;
secondMember = two;
thirdMember = three;
groupScore = firstMember.mScore + secondMember.mScore + thirdMember.mScore;
}
public double GroupScore()
{
return score;
}
}
這裏是被用來彌補本集團成員快速類:
public class Member
{
public string idNum;
public string firstName;
public string lastName;
public double mScore;
public Member()
{
}
public Member(string id, string fnm, string lnm, double pt)
{
idNum = id;
firstName = fnm;
lastName = lnm;
mScore = pt;
}
public override string ToString()
{ return firstName + " " + lastName; }
public string Desc()
{
return idNum + " " + firstName + " " + mScore.ToString();
}
}
好了,所以我重新設計它並且似乎得到一致的結果,無論列表的順序如何....仍然想知道爲什麼第一次嘗試不斷搞砸。下面是重新設計的版本:
public List<MyGroup> RemoveSimilar(List<MyGroup> group, int num)
{
List<MyGroup> temp = group.ToList();//take group argument and copy it into a new list
for (int i = 0; i < group.Count - 1; i++)//first loop through each element of group list
{
if (temp.Contains(group[i])) //checks to make sure group[i] hasn't already been removed from temp list
{
for (int j = i + 1; j < group.Count; j++)//second loop through each element to compare to first
{
if(group[i] != group[j]) //make sure we aren't comparing the same two objects
{
if (group[i].compareMembers(group[j]) > num)//check to see how "similar" groups are
{
if (group[i].score < group[j].score)//if the groups are similar, see which has a higher score
{
temp.Remove(group[i]);//outer element is not the best "unique group" so we remove it
break; //removed this one might as well stop checking it
}
else
{
temp.Remove(group[j]);//inner element was similar to outter, but not the best so we remove it
}
}
}
}
}
}
return temp; //return the list after all similar elements have been removed
}
羣組[i] .compareMembers(組[j])背後的邏輯是什麼。我認爲這應該返回0爲-1等於大於其他和1爲其他方式。你爲什麼把它和隨機數比較? – mybirthname
你可否也請包括'MyGroup'的定義來實現'compareMembers'和'score'?那是 - 我們可以簡單地複製粘貼並立即複製問題的代碼片段?匆匆一瞥,'compareMembers'可以傳遞給'A:B'和'B:C',但不適用於'A:C'。這意味着執行順序*重要*。 – Rob
comparemembers背後的邏輯是它查看兩個MyGroup對象並找出它們共有多少個「成員」。這是與允許的數量(數量)進行比較..可能是一個或兩個,等等。然後,如果他們有比我想要的更多,他們應該被刪除。基本上這應該是在大型列表中找到獨特的組合。 – marateon