Ro'yxatni maxsus qiymat bilan boshlangan ro'yxatga qanday ajratish mumkin?

Quyidagi ro'yxatga egaman.

var mainList = new List
{ "Reset","Set","Test","Test","Reset","Test","Test"};

Ushbu mainlistni ikkita ro'yxatga bo'lishni istayman, ularning har bir qismi "Nolga o'rnatish" ni boshlaydi. Kabi,

{ "Reset","Set","Test","Test"} and {"Reset","Test","Test"}

Ro'yxatni qanday ajratish mumkin?

1
Kirish formati aniqlanganmi? Demak, bu har doim ro'yxat bo'ladimi?
qo'shib qo'ydi muallif A3006, manba
Mening yechimimni sinab ko'ring va siz uchun ishlayotganini tekshiring.
qo'shib qo'ydi muallif A3006, manba
Ha, har doim List.
qo'shib qo'ydi muallif Namy, manba

7 javoblar

Agar men buni to'g'ri tushunsam, bu kabi ishlarni qilmoqchisan

var mainList = new List { "Reset", "Set", "Test", "Test", "Reset", "Test", "Test" };
List jask = new List();
List jask1 = new List();
jask = mainList.Take(4).ToList();
jask1 = mainList.Skip(4).ToList();

Keyinchalik uni "Reset" ("Nolga o'rnatish") yordamida ayirboshlashni xohlaysiz

var mainList = new List { "Reset", "Set", "Test", "Test", "Reset", "Test", "Test" };
List jask = new List();
string ksjd =  string.Join(",", mainList.ToArray());
jask = Regex.Split(ksjd, @"(?=Reset)").Skip(1).ToList();
2
qo'shib qo'ydi
Sizning ikkinchi usulingiz "Reset, Set, Test, Test", "Reset, Test, Test" ma'lumotlar bilan IEnumerable ; bu O'Z istagan narsa emas;).
qo'shib qo'ydi muallif shA.t, manba
Mohit, bu juda qattiq kodlangan ko'rinadi ...
qo'shib qo'ydi muallif A3006, manba
Rahmat Mohit. "Nolga o'rnatish" pozitsiyasi aniq emas. Shunday qilib, "Reset" indeksini o'zgartirganda, javobingiz amalga oshmaydi. .
qo'shib qo'ydi muallif Namy, manba

Quyidagi yechim sizni "Nolga o'rnatish" matnidan oldin bo'lishingizni ta'kidlaydi, shuning uchun birinchi maydon "Reset" bo'lmasa, "Reset" topilmaguncha barcha elementlarni o'tkazib yuboradi.

  public static List> Split(this List list, string splitter)
        {
            var _list = new List>();
            var count = list.Count(x => x == splitter);
            list.ForEach(item =>
            {
                if(item == splitter)
                {
                    _list.Add(new List());
                }
                _list.LastOrDefault()?.Add(item);
            });
            return _list.ToList();
        }

Foydalanish

 var mainList = new List
                          { "Set","Test","Test","Reset","Test","Test"};
 var res = mainList.Split("Reset");
1
qo'shib qo'ydi
Iltimos, foydalanadigan Null-shartli Operatorni eslayman, uning C# 6 xususiyati msdn.microsoft.com /en-in/library/dn986595.aspx
qo'shib qo'ydi muallif A.T., manba
Rahmat A.T. Bu yaxshi ishlaydi va "splitter" ni argument sifatida berish yaxshi.
qo'shib qo'ydi muallif Namy, manba
Rahmat, mening rivojlanish muhitimda C# 6 xususiyatidan foydalanishga imkon beradi.
qo'shib qo'ydi muallif Namy, manba

Shunga o'xshash narsa:

var result = string
    .Join("|", mainList)
   //    ^ use a special character/pattern that will never use in your texts
    .Replace("Reset", "|@|Reset")
   //                ^^^ use a special pattern for identifying place of `Reset`  
    .Split(new [] { "|@|" }, StringSplitOptions.RemoveEmptyEntries)
   //at first I split by place of `Reset` to create a list of strings that identifies by `|@|`
    .Select(c => c.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries))
   //now each string will become a list of strings
    .ToList();

[C # Namoyish]


LinQ-full rejimida men buni taklif qilishim mumkin:
Izoh: Thjis usuli SQLda Partition By ga o'xshash ishlaydi.

var i = 1;
var result = mainList
    .Select(str =>
    {
        if (str == "Reset") i++;
        return new { str, i };
    })
   // ^^ Above I make partitions by `i`; that `i` will change by watching a `Reset`
    .GroupBy(g => g.i)
    .Select(g => g.Select(c => c.str).ToList());
   //then I just group by `i` as partition then removing it from results.

[C # Namoyish]

0
qo'shib qo'ydi

Natijada guruhlarni olish uchun siz bu yerda lug'atni ishlatishingiz kerak, deb o'ylayman:

var mainList = new List { "Reset", "Set", "Test", "Test", "Reset", "Test", "Test" };
Dictionary> resultList = new Dictionary>();
int DictionaryIndex = 0;
foreach (string item in mainList)
{
    if (item == "Reset")
    {
        resultList.Add(++DictionaryIndex, new List() { item });
    }
    else
    {
        resultList[DictionaryIndex].Add(item);
    }
}

resultList kerak bo'lganda ajratilgan ro'yxatni o'z ichiga oladi. Siz ushbu namunasi ga kirib, sizning talablaringizni qondiradimi yoki yo'qligini bilishingiz mumkin.

0
qo'shib qo'ydi

Buni ko'ring ...

public static void SplitList()
    {
        var mainList = new List { "Reset", "Set", "Test", "Test", "Reset", "Test", "Test" };

        List> lstOutputLists = new List>();
        List tmp = new List();
        foreach (var item in mainList)
        {
            if (item == "Reset")
            {
                if (tmp.Count != 0)
                {
                    lstOutputLists.Add(tmp);
                    tmp = new List();    
                }

            }
            tmp.Add(item);
        }
        lstOutputLists.Add(tmp);
    }

lstOutputLists sizga chiqishni beradi.

0
qo'shib qo'ydi
Buni eshitganimdan xursandman ...
qo'shib qo'ydi muallif A3006, manba
A3006 uchun rahmat! SplitList() yordamida ro'yxatlarim bor. Agar ota-listda ko'proq elementlar mavjud bo'lsa, u yaxshi ishlaydi!
qo'shib qo'ydi muallif Namy, manba

Try this method: https://msdn.microsoft.com/en-us/library/1f4bkxt7(v=vs.110).aspx

// pseudocode

while (index < len)
{
  int nextOccurence = arrayList.IndexOf(delimeterString, index)
  output.Add (arrayList.GetRange(index, nextOccurence))
  index = nextOccurence + 1
}
0
qo'shib qo'ydi

qara

var mainList = new List { "Reset", "Set", "Test", "Test", "Reset",        "Test", "Test" };
        var lastIndex = mainList.FindLastIndex(x => x.Equals("Reset"));
        var firstList = new List();
        var secondList = new List();
        for (int i = 0; i < mainList.Count - 1; i++)
        {
            if (i < lastIndex)
            {
                firstList.Add(mainList[i]);
            }
            else
            {
                secondList.Add(mainList[i]);
            }
        }
0
qo'shib qo'ydi