3 回答
TA贡献1786条经验 获得超13个赞
尝试这个:
public static IEnumerable<IList<Couple>> Split(IEnumerable<Couple> couples)
{
using (var enumerator = couples.GetEnumerator())
{
if (!enumerator.MoveNext())
{
yield break;
}
var current = enumerator.Current;
var group = new List<Couple> { current };
while (enumerator.MoveNext())
{
var next = enumerator.Current;
if (current.Indicator.Equals(next.Indicator))
{
group.Add(next);
}
else
{
yield return group;
group = new List<Couple> { next };
}
current = next;
}
yield return group;
}
}
例子:
var couples = new List<Couple>
{
new Couple("a",false),
new Couple("b",false),
new Couple("c",true),
new Couple("d",false),
new Couple("e",false),
new Couple("f",true),
new Couple("g",true),
new Couple("h",true),
new Couple("i",false),
new Couple("j",true),
new Couple("k",true),
new Couple("l",false),
new Couple("m",false),
};
var groupNr = 1;
foreach (var couplesGroup in Split(couples))
{
Console.WriteLine($"List {groupNr++}: ");
foreach (var couple in couplesGroup)
{
Console.WriteLine($"{couple.Text, 10}, {couple.Indicator}");
}
Console.WriteLine();
}
TA贡献1829条经验 获得超4个赞
这是分割序列的通用扩展方法。它需要一个函数来检查两个连续的元素,并确定这些元素是否应该拆分。true意味着分裂元素的结果。均值的结果false不会拆分元素,而是将它们放在同一子序列中。
public static class EnumerableExtensions
{
/// <summary>Splits a sequence to subsequences according to a specified
/// predicate.</summary>
/// <param name="splitPredicate">A function to determine if two consecutive
/// elements should be split.</param>
public static IEnumerable<TSource[]> SplitByPredicate<TSource>(
this IEnumerable<TSource> source,
Func<TSource, TSource, bool> splitPredicate)
{
var enumerator = source.GetEnumerator();
bool finished = false;
TSource previous = default;
using (enumerator)
{
if (!enumerator.MoveNext()) yield break;
while (!finished)
{
yield return GetSubsequence().ToArray();
}
}
IEnumerable<TSource> GetSubsequence()
{
while (true)
{
yield return enumerator.Current;
previous = enumerator.Current;
if (!enumerator.MoveNext()) { finished = true; break; }
if (splitPredicate(previous, enumerator.Current)) break;
}
}
}
}
使用示例:
var subsequences = couples.SplitByPredicate(
(x, y) => x.Indicator != y.Indicator);
TA贡献1864条经验 获得超6个赞
这是一个不使用 Linq 的解决方案:
class Program
{
public class Couple
{
public string Text;
public bool Indicator;
public Couple(string text, bool indicator)
{
Text = text;
Indicator = indicator;
}
}
static void Main(string[] args)
{
var list = new List<Couple>();
list.Add(new Couple("a", false));
list.Add(new Couple("b", false));
list.Add(new Couple("c", true));
list.Add(new Couple("d", false));
list.Add(new Couple("e", false));
list.Add(new Couple("f", true));
list.Add(new Couple("g", true));
list.Add(new Couple("h", true));
list.Add(new Couple("i", false));
list.Add(new Couple("j", true));
list.Add(new Couple("k", true));
list.Add(new Couple("l", false));
list.Add(new Couple("m", false));
var result = new List<List<Couple>>();
int index = 0;
bool last = list[0].Indicator;
result.Add(new List<Couple>());
foreach ( var item in list )
{
if ( item.Indicator != last )
{
index++;
result.Add(new List<Couple>());
}
last = item.Indicator;
result[index].Add(item);
}
for ( index = 0; index < result.Count; index++ )
{
Console.WriteLine($"List n°{index}");
foreach ( var item in result[index] )
Console.WriteLine($" text: {item.Text}");
}
Console.WriteLine("");
Console.ReadKey();
}
- 3 回答
- 0 关注
- 108 浏览
添加回答
举报