例如,我有一个整数序列
1122211121
我想得到一些字典/匿名类显示:
item | count
1 | 2
2 | 3
1 | 3
2 | 1
1 | 1
var test = new[] { 1, 2, 2, 2, 2, 1, 1, 3 };
int previous = test.First();
int idx = 0;
test.Select(x =>
x == previous ?
new { orig = x, helper = idx } :
new { orig = previous = x, helper = ++idx })
.GroupBy(x => x.helper)
.Select(group => new { number = group.First().orig, count = group.Count() });
如果您想更加 Linqy,可以在let
子句中previous
和idx
初始化。
from whatever in new[] { "i want to use linq everywhere" }
let previous = test.First()
let idx = 0
from x in test
...
函数式编程很好,但恕我直言,在这种情况下,在 C# 中,我肯定会选择相当过程化的方法。
您希望在morelinq项目中执行类似"批处理"运算符的操作,然后输出组的计数。
不幸的是,morelinq 的批处理运算符只采用一个大小并返回按该大小批处理的存储桶(或者当我查看 morelinq 时确实如此)。为了纠正这个缺陷,我不得不编写自己的批处理实现。
private static IEnumerable<TResult> BatchImplementation<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TSource, int, bool> breakCondition,
Func<IEnumerable<TSource>, TResult> resultSelector
)
{
List<TSource> bucket = null;
var lastItem = default(TSource);
var count = 0;
foreach (var item in source)
{
if (breakCondition(item, lastItem, count++))
{
if (bucket != null)
{
yield return resultSelector(bucket.Select(x => x));
}
bucket = new List<TSource>();
}
bucket.Add(item);
lastItem = item;
}
// Return the last bucket with all remaining elements
if (bucket.Count > 0)
{
yield return resultSelector(bucket.Select(x => x));
}
}
这是我公开各种验证输入参数的公共重载的私有版本。你会希望你的breakCondition是这样的形式:
Func<int, int, int, bool> breakCondition = x, y, z => x != y;
对于您的示例序列,这应该为您提供:{1, 1}, {2, 2, 2}, {1, 1, 1}, {2}, {1}
从这里开始,抓住每个序列的第一项,然后计算序列是微不足道的。
编辑:协助实施 -
public static IEnumerable<IEnumerable<TSource>> Batch<TSource>(
this IEnumerable<TSource> source,
Func<TSource, TSource, int, bool> breakCondition
)
{
//Validate that source, breakCondition, and resultSelector are not null
return BatchImplemenatation(source, breakCondition, x => x);
}
然后,您的代码将是:
var sequence = {1, 1, 2, 2, 2, 1, 1, 1, 2, 1};
var batchedSequence = sequence.batch((x, y, z) => x != y);
//batchedSequence = {{1, 1}, {2, 2, 2}, {1, 1, 1}, {2}, {1}}
var counts = batchedSequence.Select(x => x.Count());
//counts = {2, 3, 3, 1, 1}
var items = batchedSequence.Select(x => x.First());
//items = {1, 2, 1, 2, 1}
var final = counts.Zip(items. (c, i) => {Item = i, Count = c});
除了私有方法及其在自己的代码库中使用的重载之外,我还没有编译和测试任何内容,但这应该可以解决您的问题以及您遇到的任何类似问题。
Wel...短一点(注意处理偶数/奇数计数的双重单独调用):
static void Main(string[] args)
{
string separatedDigits = Separate(Separate("1122211121"));
foreach (var ano in separatedDigits.Split('|').Select(block => new { item = block.Substring(0, 1), count = block.Length }))
Console.WriteLine(ano);
Console.ReadKey();
}
static string Separate(string input)
{
return Regex.Replace(input, @"(d)(?!1)(d)", "$1|$2");
}
}