请问,这个集合,如何去除重复元素

RT3254 2011-10-25 05:12:30
List<List<int>> list = new List<List<int>>()
{
new List<int>() { 0, 1, 2 },
new List<int>() { 0, 1, 2 },
new List<int>() { 1, 3,8 }
};

如何把相同的去除,很显然,有两个{ 0, 1, 2 }

list.Distinct().....不见效果
...全文
304 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2011-10-26
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 stonespace 的回复:]
他写错了,x应该是obj,

其实List.Distinct效率很低,元素多了,建议还是用HashSet去检查重复,


引用 13 楼 rt3254 的回复:

引用 9 楼 caozhy 的回复:

C# code
list = list.Distinct(new StringListComparer());

class StringListComparer : IE……
[/Quote]
Distinct内部使用GetHashCode而不是Equals,所以效率等同HashSet。
xlong224 2011-10-26
  • 打赏
  • 举报
回复
学习。
stonespace 2011-10-26
  • 打赏
  • 举报
回复
他写错了,x应该是obj,

其实List.Distinct效率很低,元素多了,建议还是用HashSet去检查重复,

[Quote=引用 13 楼 rt3254 的回复:]

引用 9 楼 caozhy 的回复:

C# code
list = list.Distinct(new StringListComparer());

class StringListComparer : IEqualityComparer<List<string>>
{
public bool Equals(List<string> x, List<string> y)
{
……
[/Quote]
mingcsharp 2011-10-26
  • 打赏
  • 举报
回复
就是一个钦套吗?
ailin84 2011-10-26
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 caozhy 的回复:]
C# code

list = list.Distinct(new StringListComparer());

class StringListComparer : IEqualityComparer<List<string>>
{
public bool Equals(List<string> x, List<string> y)
{
retu……
[/Quote]


学习了
RT3254 2011-10-25
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 caozhy 的回复:]

C# code
list = list.Distinct(new StringListComparer());

class StringListComparer : IEqualityComparer<List<string>>
{
public bool Equals(List<string> x, List<string> y)
{
return stri……
[/Quote]
感谢,个位朋友的指教,这段时间,很多东西没弄明白,明白之后,我会一个一个的结贴,认真的结贴
始终对GetHashCode方法不太明白,正如:
public int GetHashCode(List<string> obj)
{
return string.Join(",", x.OrderBy(a => a)).GetHashCode();
}
参数x是怎么来的呢?x不是Equals方法的参数吗?GetHashCode方法没参数x啊?
ajiangfeijun 2011-10-25
  • 打赏
  • 举报
回复
都是高手
阿非 2011-10-25
  • 打赏
  • 举报
回复

public class CustomIntListEqualityComparer : IEqualityComparer<List<int>>
{
public bool Equals(List<int> x, List<int> y)
{
//x==y==null
if (x == y)
return true;
if (x == null || y == null)
return false;
if (x.Count != y.Count)
return false;
for (int i = 0, count = x.Count; i < count; i++)
{
if (x[i] != y[i])
return false;
}
return true;
//throw new NotImplementedException();
}

public int GetHashCode(List<int> obj)
{
if (obj == null)
return 0;
int hash = 17;
hash = (hash + obj.Count) * 3;
return obj.Aggregate(hash, (start, next) => (hash = (hash + next) * 3));
//throw new NotImplementedException();
}
}

List<List<int>> list = new List<List<int>>()
{
new List<int>() { 0, 1, 2 },
new List<int>() { 0, 2, 1 },
new List<int>() { 2, 1, 0 },
new List<int>() { 0, 1, 2 },
new List<int>() { 2, 0, 1 },
new List<int>() { 1, 3, 8 }
};

foreach (var r in list.Distinct(new CustomIntListEqualityComparer()))
{
Console.WriteLine(string.Join(",", r.Select(e => string.Format("\"{0}\"", e.ToString()))));
}

黄亮 2011-10-25
  • 打赏
  • 举报
回复

var list = new List<List<int>>
{
new List<int> {0, 1, 2},
new List<int> {0, 1, 2},
new List<int> {1, 3, 8}
};
list = list.Distinct(new MyListComparer()).ToList();


internal class MyListComparer : IEqualityComparer<List<int>>
{
#region IEqualityComparer<List<int>> Members

public bool Equals(List<int> x, List<int> y)
{
x.Sort();
y.Sort();
if (x.Count != y.Count)
return false;
return !x.Where((t, index) => y[index] != t).Any();
}

public int GetHashCode(List<int> obj)
{
return obj.Aggregate(1, (current, i) => current*i);
}

#endregion
}
threenewbee 2011-10-25
  • 打赏
  • 举报
回复
list = list.Distinct(new StringListComparer());

class StringListComparer : IEqualityComparer<List<string>>
{
public bool Equals(List<string> x, List<string> y)
{
return string.Join(",", x.OrderBy(a => a)) == string.Join(",", y.OrderBy(a => a));
}

public int GetHashCode(List<string> obj)
{
return string.Join(",", x.OrderBy(a => a)).GetHashCode();
}
}
zzz9413 2011-10-25
  • 打赏
  • 举报
回复
listnew即去掉重复之后的集合
zzz9413 2011-10-25
  • 打赏
  • 举报
回复
上面是随便写的,发现没想象中那么容易,补充正确答案,已经通过测试
正确答案如下:
两个辅助方法

/// <summary>
/// 把List<int>转换成string
/// </summary>
/// <param name="listint"></param>
/// <returns></returns>
string getStringByInt(List<int> listint)
{
string str = "";
foreach (int item in listint)
{
str += item.ToString();
}
return str;
}
/// <summary>
/// 判断ListListint是否包含str字符串
/// </summary>
/// <param name="listnew"></param>
/// <param name="str"></param>
/// <returns></returns>
bool iscon(List<List<int>> listnew,string str)
{
foreach (List<int> item in listnew)
{
if (getStringByInt(item) == str)
{
return true;
}

}
return false;
}

主程序:

List<List<int>> list = new List<List<int>>()
{

new List<int>() { 0, 1, 2 },

new List<int>() { 0, 1, 2 },

new List<int>() { 1, 3,8 }

};
List<List<int>> listnew = new List<List<int>>();
foreach (List<int> item in list)
{
string str = getStringByInt(item);
if (!iscon(listnew, str))
{
listnew.Add(item);
}
}
MessageBox.Show("listnew数目:" + listnew.Count);
zzz9413 2011-10-25
  • 打赏
  • 举报
回复

List<List<int>> list = new List<List<int>>()
{

new List<int>() { 0, 1, 2 },

new List<int>() { 0, 1, 2 },

new List<int>() { 1, 3,8 }

};
List<List<int>> listnew = new List<List<int>>();
for (int i = 0; i < list.Count; i++)
{
if (iscon(listnew, list[i]))//自己写个iscon方法判断list[i]是否在listnew内
{
list.Remove(list[i]);
}
else
{
listnew.Add(list[i]);
}
}

小童 2011-10-25
  • 打赏
  • 举报
回复
看看List.Find();
sdl2005lyx 2011-10-25
  • 打赏
  • 举报
回复
在加入的时候判断更合理些。。。
wnyxy001 2011-10-25
  • 打赏
  • 举报
回复
好像也没效果
wnyxy001 2011-10-25
  • 打赏
  • 举报
回复

List<List<int>> list = new List<List<int>>()
{
new List<int>() { 0, 1, 2 },
new List<int>() { 0, 1, 2 },
new List<int>() { 1, 3,8 }
};

List<List<int>> list1 = new List<List<int>>();
foreach(List<int> list0 in list)
{
if (!list1.Contains(list0))
{
list1.Add(list0);
}
}
wnyxy001 2011-10-25
  • 打赏
  • 举报
回复

List<List<int>> list1 = new List<List<int>>();
foreach(List<int> list0 in list1)
{
if (!list1.Contains(list0))
{
list1.Add(list0);
}
}

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧