计算列表<T><列表中顺序相关列表的出现次数<T>>



还有一个类似的问题不能回答我的问题。-->列表>中元素的计数

我有一个包含子列表的列表:

List<string> sublist1 = new List<string>() { "a", "b" };
List<string> sublist2 = new List<string>() { "a", "b" };
List<string> sublist3 = new List<string>() { "a", "c" };

现在我想统计每个列表的出现次数。

a, b --> 2
a, c --> 1

我使用了LINQ中的distinct(),但得到了输出:

a, b --> 1
a, b --> 1
a, c --> 1

我认为散列码是不同的。是否有distinct()的替代方案,即改为查看列表值?如果可能的话,我想在LINQ中解决这个问题。

编辑:列表项的顺序必须相同!

要使用GroupBy()来执行此操作,您需要一个合适的IEqualityComparer<List<string>>来比较字符串列表。没有内置的实现,所以你必须推出自己的:

public sealed class StringListEqualityComparer : IEqualityComparer<List<string>>
{
public bool Equals(List<string> x, List<string> y)
{
if (ReferenceEquals(x, y))
return true;
if (x == null || y == null)
return false;
return x.SequenceEqual(y);
}
public int GetHashCode(List<string> strings)
{
int hash = 17;
foreach (var s in strings)
{
unchecked
{
hash = hash * 23 + s?.GetHashCode() ?? 0;
}
}
return hash;
}
}

一旦你有了它,你可以将它与GroupBy()一起使用,如下所示:

public static void Main()
{
var sublist1 = new List<string>{ "a", "b" };
var sublist2 = new List<string>{ "a", "b" };
var sublist3 = new List<string>{ "a", "c" };
var listOfLists = new List<List<string>> {sublist1, sublist2, sublist3};
var groups = listOfLists.GroupBy(item => item, new StringListEqualityComparer());
foreach (var group in groups)
{
Console.WriteLine($"Group: {string.Join(", ", group.Key)}, Count: {group.Count()}");
}
}
public JsonResult CountList(){
List<List<string>> d = new List<List<string>>(); //SuperList
d.Add(new List<string> { "a", "b" }); //List 1
d.Add(new List<string> { "a", "b" }); // List 2
d.Add(new List<string> { "a", "c" }); // List 3
d.Add(new List<string> { "a", "c", "z" }); //List 4
var listCount = from items in d
group items by items.Aggregate((a,b)=>a+""+b) into groups
select new { groups.Key, Count = groups.Count() };
return new JsonResult(listCount);
}

这将在Post Man或Advanced REST Client 中输出以下结果

[{
"key": "ab",
"count": 2
},
{
"key": "ac",
"count": 1
},
{
"key": "acz",
"count": 1
}],

我认为这将对有所帮助

var list = new List<List<string>>() { sublist1, sublist2, sublist3};
var result = list.GroupBy(x => string.Join(",",x)).ToDictionary(x => x.Key.Split(',').ToList(), x => x.Count());

您可以尝试以下代码:-

List<string> sublist1 = new List<string>() { "a", "b" };   
List<string> sublist2 = new List<string>() { "a", "b" };   
List<string> sublist3 = new List<string>() { "a", "c" };   
List<List<string>> listOfLists = new List<List<string>> { sublist1, sublist2, sublist3 };    
Dictionary<string, int> counterDictionary = new Dictionary<string, int>();                  
foreach (List<string> strList in listOfLists)       
{   
string concat = strList.Aggregate((s1, s2) => s1 + ", " + s2);   
if (!counterDictionary.ContainsKey(concat))   
counterDictionary.Add(concat, 1);   
else   
counterDictionary[concat] = counterDictionary[concat] + 1;   
}   
foreach (KeyValuePair<string, int> keyValue in counterDictionary)   
{   
Console.WriteLine(keyValue.Key + "=>" + keyValue.Value);   
}   

我想我会用解决这个问题

var equallists = list1.SequenceEqual(list2);

因此,我使用SequenceEquals((比较不同的列表和列表,并对它们进行计数。

欢迎更好的解决方案。:(

相关内容

  • 没有找到相关文章

最新更新