我试图将列表拆分成一系列较小的列表.
我的问题:我拆分列表的功能不会将它们拆分成正确大小的列表.它应该将它们分成大小为30的列表,而是将它们分成大小为114的列表?
如何使我的功能将列表拆分为X个大小为30或更小的列表?
public static List<List<float[]>> splitList(List <float[]> locations, int nSize=30)
{
List<List<float[]>> list = new List<List<float[]>>();
for (int i=(int)(Math.Ceiling((decimal)(locations.Count/nSize))); i>=0; i--) {
List <float[]> subLocat = new List <float[]>(locations);
if (subLocat.Count >= ((i*nSize)+nSize))
subLocat.RemoveRange(i*nSize, nSize);
else subLocat.RemoveRange(i*nSize, subLocat.Count-(i*nSize));
Debug.Log ("Index: "+i.ToString()+", Size: "+subLocat.Count.ToString());
list.Add (subLocat);
}
return list;
}
Run Code Online (Sandbox Code Playgroud)
如果我在144的列表上使用该函数,那么输出是:
指数:4,大小:120
指数:3,大小:114
索引:2,大小:114
索引:1,大小:114
索引:0,大小:114
Dmi*_*lov 351
我建议使用此扩展方法将源列表按指定的块大小分块到子列表:
/// <summary>
/// Helper methods for the lists.
/// </summary>
public static class ListExtensions
{
public static List<List<T>> ChunkBy<T>(this List<T> source, int chunkSize)
{
return source
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / chunkSize)
.Select(x => x.Select(v => v.Value).ToList())
.ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
例如,如果您按照每个块的5个项目清除18个项目的列表,它会为您提供4个子列表的列表,其中包含以下项目:5-5-5-3.
Ser*_*-Tm 226
public static List<List<float[]>> splitList(List<float[]> locations, int nSize=30)
{
var list = new List<List<float[]>>();
for (int i = 0; i < locations.Count; i += nSize)
{
list.Add(locations.GetRange(i, Math.Min(nSize, locations.Count - i)));
}
return list;
}
Run Code Online (Sandbox Code Playgroud)
通用版本:
public static IEnumerable<List<T>> splitList<T>(List<T> locations, int nSize=30)
{
for (int i = 0; i < locations.Count; i += nSize)
{
yield return locations.GetRange(i, Math.Min(nSize, locations.Count - i));
}
}
Run Code Online (Sandbox Code Playgroud)
Raf*_*fal 33
怎么样:
while(locations.Any())
{
list.Add(locations.Take(nSize).ToList());
locations= locations.Skip(nSize).ToList();
}
Run Code Online (Sandbox Code Playgroud)
equ*_*tas 10
Serj-Tm解决方案很好,这也是通用版本作为列表的扩展方法(把它放到静态类中):
public static List<List<T>> Split<T>(this List<T> items, int sliceSize = 30)
{
List<List<T>> list = new List<List<T>>();
for (int i = 0; i < items.Count; i += sliceSize)
list.Add(items.GetRange(i, Math.Min(sliceSize, items.Count - i)));
return list;
}
Run Code Online (Sandbox Code Playgroud)
我发现接受的答案(Serj-Tm)最强大,但我想建议一个通用版本.
public static List<List<T>> splitList<T>(List<T> locations, int nSize = 30)
{
var list = new List<List<T>>();
for (int i = 0; i < locations.Count; i += nSize)
{
list.Add(locations.GetRange(i, Math.Min(nSize, locations.Count - i)));
}
return list;
}
Run Code Online (Sandbox Code Playgroud)
在最后 mhand 的非常有用的评论之后添加
尽管大多数解决方案可能有效,但我认为它们不是很有效。假设您只想要前几个块的前几个项目。那么您就不想遍历序列中的所有(无数)项。
以下将最多枚举两次:一次用于 Take,一次用于 Skip。它不会枚举比您将使用的元素更多的元素:
public static IEnumerable<IEnumerable<TSource>> ChunkBy<TSource>
(this IEnumerable<TSource> source, int chunkSize)
{
while (source.Any()) // while there are elements left
{ // still something to chunk:
yield return source.Take(chunkSize); // return a chunk of chunkSize
source = source.Skip(chunkSize); // skip the returned chunk
}
}
Run Code Online (Sandbox Code Playgroud)
假设您将源分为chunkSize
. 您仅枚举前 N 个块。从每个枚举块中,您将只枚举前 M 个元素。
While(source.Any())
{
...
}
Run Code Online (Sandbox Code Playgroud)
Any 将获取枚举器,执行 1 MoveNext() 并在处理枚举器后返回返回值。这将做N次
yield return source.Take(chunkSize);
Run Code Online (Sandbox Code Playgroud)
根据参考来源,这将执行以下操作:
public static IEnumerable<TSource> Take<TSource>(this IEnumerable<TSource> source, int count)
{
return TakeIterator<TSource>(source, count);
}
static IEnumerable<TSource> TakeIterator<TSource>(IEnumerable<TSource> source, int count)
{
foreach (TSource element in source)
{
yield return element;
if (--count == 0) break;
}
}
Run Code Online (Sandbox Code Playgroud)
在您开始枚举获取的 Chunk 之前,这不会做很多事情。如果您获取多个块,但决定不枚举第一个块,则不会执行 foreach,因为您的调试器会显示给您。
如果您决定采用第一个块的前 M 个元素,则 yield return 将执行 M 次。这意味着:
在返回第一个块后,我们跳过第一个块:
source = source.Skip(chunkSize);
Run Code Online (Sandbox Code Playgroud)
再次:我们将查看参考源以找到skipiterator
static IEnumerable<TSource> SkipIterator<TSource>(IEnumerable<TSource> source, int count)
{
using (IEnumerator<TSource> e = source.GetEnumerator())
{
while (count > 0 && e.MoveNext()) count--;
if (count <= 0)
{
while (e.MoveNext()) yield return e.Current;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,对 Chunk 中的每个元素SkipIterator
调用MoveNext()
一次。它不会调用Current
.
因此,每个 Chunk 我们看到完成了以下操作:
拿():
如果枚举内容:GetEnumerator(),每个枚举项一个 MoveNext 和一个 Current,Dispose enumerator;
Skip():对于枚举的每个块(不是块的内容):GetEnumerator(), MoveNext() chunkSize 次,没有 Current!配置枚举器
如果您查看枚举器发生的情况,您会发现有很多对 MoveNext() 的调用,并且只调用了Current
您实际决定访问的 TSource 项。
如果取 N 个大小为 chunkSize 的块,则调用 MoveNext()
如果您决定仅枚举每个获取的块的前 M 个元素,那么您需要为每个枚举的块调用 MoveNext M 次。
总数
MoveNext calls: N + N*M + N*chunkSize
Current calls: N*M; (only the items you really access)
Run Code Online (Sandbox Code Playgroud)
因此,如果您决定枚举所有块的所有元素:
MoveNext: numberOfChunks + all elements + all elements = about twice the sequence
Current: every item is accessed exactly once
Run Code Online (Sandbox Code Playgroud)
MoveNext 是否需要大量工作,取决于源序列的类型。对于列表和数组,它是一个简单的索引增量,可能带有超出范围的检查。
但是如果你的 IEnumerable 是数据库查询的结果,请确保数据确实在你的计算机上具体化,否则数据将被多次获取。DbContext 和 Dapper 会在数据被访问之前正确地将数据传输到本地进程。如果多次枚举相同的序列,则不会多次提取。Dapper 返回一个 List 对象,DbContext 记住数据已经被获取。
在开始划分 Chunks 中的项目之前调用 AsEnumerable() 或 ToLists() 是否明智取决于您的存储库
.NET 6 之前
public static IEnumerable<IEnumerable<T>> SplitIntoSets<T>
(this IEnumerable<T> source, int itemsPerSet)
{
var sourceList = source as List<T> ?? source.ToList();
for (var index = 0; index < sourceList.Count; index += itemsPerSet)
{
yield return sourceList.Skip(index).Take(itemsPerSet);
}
}
Run Code Online (Sandbox Code Playgroud)
.NET 6
var originalList = new List<int>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
// split into arrays of no more than three
IEnumerable<int[]> chunks = originalList.originalList.Chunk(3);
Run Code Online (Sandbox Code Playgroud)
我有一个通用的方法,可以采取任何类型包括浮点数,它已经过单元测试,希望它有所帮助:
/// <summary>
/// Breaks the list into groups with each group containing no more than the specified group size
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="values">The values.</param>
/// <param name="groupSize">Size of the group.</param>
/// <returns></returns>
public static List<List<T>> SplitList<T>(IEnumerable<T> values, int groupSize, int? maxCount = null)
{
List<List<T>> result = new List<List<T>>();
// Quick and special scenario
if (values.Count() <= groupSize)
{
result.Add(values.ToList());
}
else
{
List<T> valueList = values.ToList();
int startIndex = 0;
int count = valueList.Count;
int elementCount = 0;
while (startIndex < count && (!maxCount.HasValue || (maxCount.HasValue && startIndex < maxCount)))
{
elementCount = (startIndex + groupSize > count) ? count - startIndex : groupSize;
result.Add(valueList.GetRange(startIndex, elementCount));
startIndex += elementCount;
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
图书馆MoreLinq的方法称为 Batch
List<int> ids = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; // 10 elements
int counter = 1;
foreach(var batch in ids.Batch(2))
{
foreach(var eachId in batch)
{
Console.WriteLine("Batch: {0}, Id: {1}", counter, eachId);
}
counter++;
}
Run Code Online (Sandbox Code Playgroud)
结果是
Batch: 1, Id: 1
Batch: 1, Id: 2
Batch: 2, Id: 3
Batch: 2, Id: 4
Batch: 3, Id: 5
Batch: 3, Id: 6
Batch: 4, Id: 7
Batch: 4, Id: 8
Batch: 5, Id: 9
Batch: 5, Id: 0
Run Code Online (Sandbox Code Playgroud)
ids
分为2个元素的5个大块。
尽管上面的许多答案都能胜任,但它们都以永无止境的顺序(或很长的顺序)严重失败。以下是一个完全在线的实现,可以保证最佳的时间和内存复杂性。我们仅将源可枚举迭代一次,然后使用yield return进行惰性评估。消费者可以在每次迭代时丢弃该列表,从而使内存占用量batchSize
与元素数量相等的内存占用量相等。
public static IEnumerable<List<T>> BatchBy<T>(this IEnumerable<T> enumerable, int batchSize)
{
using (var enumerator = enumerable.GetEnumerator())
{
List<T> list = null;
while (enumerator.MoveNext())
{
if (list == null)
{
list = new List<T> {enumerator.Current};
}
else if (list.Count < batchSize)
{
list.Add(enumerator.Current);
}
else
{
yield return list;
list = new List<T> {enumerator.Current};
}
}
if (list?.Count > 0)
{
yield return list;
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:刚刚意识到OP要求将a List<T>
分成更小的List<T>
,所以我对无限可枚举的评论不适用于OP,但可能会帮助到这里的其他人。这些评论是对其他发布的解决方案的回应,这些解决方案确实IEnumerable<T>
用作其功能的输入,但多次枚举了可枚举的来源。
从 .NET 6.0 开始,您可以使用 LINQ 扩展Chunk<T>()
将枚举拆分为块。文档
var chars = new List<char>() { 'h', 'e', 'l', 'l', 'o', 'w','o','r' ,'l','d' };
foreach (var batch in chars.Chunk(2))
{
foreach (var ch in batch)
{
// iterates 2 letters at a time
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
168293 次 |
最近记录: |