我正在尝试将一个集合拆分成多个集合,同时保持我对集合的排序。我尝试使用以下扩展方法,但它错误地打破了它们。基本上,如果我要查看集合中的项目,与加入的分解集合相比,顺序应该相同。这是我正在使用的代码不起作用:
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> list, int parts)
{
int i = 0;
var splits = from name in list
group name by i++ % parts into part
select part.AsEnumerable();
return splits;
}
答案 0 :(得分:10)
我不得不利用这个来比较4个组中的对象列表... 它会使对象保持原始拥有的顺序。可以扩展为“List”之外的其他内容
/// <summary>
/// Partition a list of elements into a smaller group of elements
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="totalPartitions"></param>
/// <returns></returns>
public static List<T>[] Partition<T>(List<T> list, int totalPartitions)
{
if (list == null)
throw new ArgumentNullException("list");
if (totalPartitions < 1)
throw new ArgumentOutOfRangeException("totalPartitions");
List<T>[] partitions = new List<T>[totalPartitions];
int maxSize = (int)Math.Ceiling(list.Count / (double)totalPartitions);
int k = 0;
for (int i = 0; i < partitions.Length; i++)
{
partitions[i] = new List<T>();
for (int j = k; j < k + maxSize; j++)
{
if (j >= list.Count)
break;
partitions[i].Add(list[j]);
}
k += maxSize;
}
return partitions;
}
答案 1 :(得分:4)
一种稍微干净的LINQ方法:
public static IEnumerable<IEnumerable<T>> Partition<T>(this IEnumerable<T> source, int n)
{
var count = source.Count();
return source.Select((x, i) => new { value = x, index = i })
.GroupBy(x => x.index / (int)Math.Ceiling(count / (double)n))
.Select(x => x.Select(z => z.value));
}
答案 2 :(得分:2)
Jon Skeet的MoreLINQ库可能会为您提供帮助:
https://code.google.com/p/morelinq/source/browse/MoreLinq/Batch.cs
var items = list.Batch(parts); // gives you IEnumerable<IEnumerable<T>>
var items = list.Batch(parts, seq => seq.ToList()); // gives you IEnumerable<List<T>>
// etc...
另一个例子:
public class Program
{
static void Main(string[] args)
{
var list = new List<int>();
for (int i = 1; i < 10000; i++)
{
list.Add(i);
}
var batched = list.Batch(681);
// will print 15. The 15th element has 465 items...
Console.WriteLine(batched.Count().ToString());
Console.WriteLine(batched.ElementAt(14).Count().ToString());
Console.WriteLine();
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
}
当我扫描批次的内容时,订单被保留。
答案 3 :(得分:1)
double partLength = list.Count() / (double)parts;
int i = 0;
var splits = from name in list
group name by Math.Floor((double)(i++ / partLength)) into part
select part;
答案 4 :(得分:0)
据我所知,你希望在相同大小的几个部分上打破可枚举,而不会破坏元素的顺序。看起来唯一的选择是首先可以枚举输入的长度,因此您需要至少两次迭代才能通过可枚举。
答案 5 :(得分:0)
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> list, int parts)
{
int nGroups = (int)Math.Ceiling(list.Count() / (double)parts);
var groups = Enumerable.Range(0, nGroups);
return groups.Select(g => list.Skip(g * parts).Take(parts));
}
答案 6 :(得分:0)
这将完全按照要求执行。它也将迎合不均匀的分组,即10个组中的27个元素将产生3组3组和3组2个
public static IEnumerable<IEnumerable<T>> SplitMaintainingOrder<T>(this IEnumerable<T> list, int parts)
{
if (list.Count() == 0) return Enumerable.Empty<IEnumerable<T>>();
var toreturn = new List<IEnumerable<T>>();
var splitFactor = Decimal.Divide((decimal)list.Count(), parts);
int currentIndex = 0;
for (var i = 0; i < parts; i++)
{
var toTake = Convert.ToInt32(
i == 0 ? Math.Ceiling(splitFactor) : (
(Decimal.Compare(Decimal.Divide(Convert.ToDecimal(currentIndex), Convert.ToDecimal(i)), splitFactor) > 0) ?
Math.Floor(splitFactor) : Math.Ceiling(splitFactor)));
toreturn.Add(list.Skip(currentIndex).Take(toTake));
currentIndex += toTake;
}
return toreturn;
}
出于演示目的
[TestMethod]
public void splitlist()
{
var list = new decimal[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27 };
var splitlists = list.SplitMaintainingOrder(10);
int i = 1;
foreach (var group in splitlists)
{
Console.WriteLine("Group {0} elements {1}", i++, String.Join(",", group));
}
}
以上演示产生
Test Name: splitlist
Test Outcome: Passed
Result StandardOutput:
Group 1 elements 1,2,3
Group 2 elements 4,5
Group 3 elements 6,7,8
Group 4 elements 9,10,11
Group 5 elements 12,13
Group 6 elements 14,15,16
Group 7 elements 17,18,19
Group 8 elements 20,21
Group 9 elements 22,23,24
Group 10 elements 25,26,27
答案 7 :(得分:0)
要将通用列表拆分为相等的块,请使用以下通用方法
private IEnumerable<IEnumerable<T>> SplitMaintainingOrder<T>(IEnumerable<T> list, int columnCount)
{
var elementsCount = list.Count();
int rowCount = elementsCount / columnCount;
int noOfCells = elementsCount % columnCount;
int finalRowCount = rowCount;
if (noOfCells > 0)
{
finalRowCount++;
}
var toreturn = new List<IEnumerable<T>>();
var pushto = 0;
for (int j = 0; j < columnCount; j++)
{
var start = j;
int i = 0;
var end = i;
for (i = 0; i < finalRowCount; i++)
{
if ((i < rowCount) || ((i == rowCount) && (j < noOfCells)))
{
start = j;
end = i;
}
}
toreturn.Add(list.Skip(pushto).Take(end + 1));
pushto += end + 1;
}
return toreturn;
}
&#13;
List<int> recordNumbers = new List<int>() { 1, 2, 3, 4, 5, 6,7,8,9,10,11};
var splitedItems = SplitMaintainingOrder<int>(recordNumbers , 4);
&#13;
Output will be:
List 1 : 1,2,3
List 2 : 4,5,6
List 3 : 7,8,9
List 4 : 10,11
&#13;
〜快乐的编码..