简体   繁体   English

将一个列表中的8个元素拆分为另一个C#的第一个元素

[英]Splitting 8 elements from one list into the first element of another C#

one a integer list and one a string list. 一个是整数列表,另一个是字符串列表。 The integer list's length will always be a multiple of 8. I would like to put the first 8 integers from my integer list into the first element of a string list, then loop and put the next 8 into the second element of the string list and so on. 整数列表的长度将始终是8的倍数。我想将我的整数列表中的前8个整数放入字符串列表的第一个元素,然后循环并将下一个8放入字符串列表的第二个元素,然后以此类推。 I have made an attempt, I currently have an error on the Add method as string doesn't have an add extension? 我已经尝试过,由于字符串没有添加扩展名,我目前在Add方法上遇到错误? Also I'm not sure if the way I have done it using loops is correct, any advice would be helpful. 另外我不确定使用循环的方式是否正确,任何建议都将有所帮助。

List1 is my integer list List1是我的整数列表

List2 is my string list List2是我的字符串列表

string x = "";
            for (int i = 0; i < List1.Count/8; i++) {

                for(int i2 = 0; i2 < i2+8; i2+=8)
                {
                    x = Convert.ToString(List1[i2]);

                    List2[i].Add(h);
                } 
        }

You can do that by using something like that 您可以使用类似的方法

var list1 = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
var list2 = new List<string>();

for (int i = 0; i < list1.Count / 8; i++)
{
    list2.Add(string.Concat(list1.Skip(i * 8).Take(8)));
}

// list2[0] = "12345678"
// list2[1] = "910111213141516"

A slightly more complicated approach, which only iterates once over list1 (would work with IEnumerable would be sth. like this: 稍微复杂一点的方法,它仅在list1迭代一次(将与IEnumerable一起使用将是这样的:

var list1 = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }.AsEnumerable();
var list2 = new List<string>();

var i = 0;
var nextValue = new StringBuilder();

foreach (var integer in list1)
{
    nextValue.Append(integer);
    i++;

    if (i != 0 && i % 8 == 0)
    {
        list2.Add(nextValue.ToString());
        nextValue.Clear();
    }
}

// could add remaining items if count of list1 is not a multiple of 8
// if (nextValue.Length > 0)
// {
//     list2.Add(nextValue.ToString());
// }

For the fun of it, you can implement your own general purpose Batch extension method. 有趣的是,您可以实现自己的通用Batch扩展方法。 Good practice to understand extension methods , enumerators , iterators , generics and c#'s local functions : 了解扩展方法枚举 迭代器泛型和c#的局部函数的良好实践:

static IEnumerable<IEnumerable<T>> Batch<T>(
    this IEnumerable<T> source, 
    int batchCount,
    bool throwOnPartialBatch = false)
{
    IEnumerable<T> nextBatch(IEnumerator<T> enumerator)
    {
        var counter = 0;

        do
        {
           yield return enumerator.Current;
           counter += 1;

        } while (counter < batchCount && enumerator.MoveNext());

        if (throwOnPartialBatch && counter != batchCount) //numers.Count % batchCount is not zero.
            throw new InvalidOperationException("Invalid batch size.");
    }

    if (source == null)
        throw new ArgumentNullException(nameof(source));

    if (batchCount < 1)
        throw new ArgumentOutOfRangeException(nameof(batchCount));

    using (var e = source.GetEnumerator())
    {
        while (e.MoveNext())
        {
            yield return nextBatch(e);
        }
    }
}

Using it is rather trivial: 使用它相当简单:

var ii = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
var ss = ii.Batch(4, true)
           .Select(b => string.Join(", ", b))

And sure enough, the output is: 确实,输出为:

1, 2, 3, 4
5, 6, 7, 8
9, 10, 11, 12
while (listOfintergers.Count() > 0)
        {
            var first8elements = listOfintergers.ConvertAll(t=>t.ToString()).Take(8);
            listOfStrings.Add(string.Concat(first8elements));
            listOfintergers = listOfintergers.Skip(8).ToList();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM