如何在List< string>中对字符串类型的项进行分组 [英] How to group items of type string in List<string>

查看:463
本文介绍了如何在List< string>中对字符串类型的项进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i有

Hi guys,
i have a

List<string>

包含S1-00-1001类型的元素S1- 00-1002依此类推,直至S1-00-1200连续,

然后其他元素S1-00-1501S1-00-1502依此类推至S1 -00-1600连续和许多其他元素。



一般来说我希望我的输出如下:


$ S1 $ b票从S1-00-1001到S1-00-1200

票从S1-00-1501到S1-00-1600

........

........



如何计算套装顺序字符串?

我希望你理解我的问题。

非常感谢任何回复,对不起我的英语。

that contains elements of type "S1-00-1001" "S1-00-1002" and so on up to "S1-00-1200" consecutive,
then other element "S1-00-1501" "S1-00-1502" and so on up to "S1-00-1600" consecutive and many other elements.

In general i want that my output would be like:

ticket from "S1-00-1001" to "S1-00-1200"
ticket from "S1-00-1501" to "S1-00-1600"
........
........

How I can calculate the set of the sequential string?
I hope that you understand my problem.
Many thanks for any response, sorry for my english.

推荐答案

假设所有字符串的格式总是相同(并使用伪代码):



Supposing all strings are always equal in format (and using pseudo code):

declare a variable to save the last checked number

make a loop that go through the list
   get the current element
   extract the last block of numbers
   transform the last block of numbers into a number
   if it is the first iteration of the loop
      start a group and save the actual string in it
      save the converted number into the variable declared before
      go to next iteration

   if it is not the first iteration of the loop
      if there is no saved value in the variable
         start a group and save the actual string in it
         save the converted number into the variable declared before
         go to next iteration
      else
         compare the converted number with (last number saved + 1)
         // if consecutive can be both directions, then you have to compare it |saved - current| = 1

         if the comparison of consecutive is successful
            save the current string into the actual group
            save the current number into the variable to check the next one
            go to the next iteration
         else
            end your actual group
            start a new one and add the current string in it
            go to next iteration

   when iteration ended
      give the results you need


我确定这不是最好的方法,但它有效,也许它会给你一个提示它或者找到你自己的方法;



首先,让我们用虚拟值填充列表;



I'm sure it is not best way but it is working, maybe it gives you a hint to fix it or find your own method;

First, let's fill list with dummy values;

List<string> list = new List<string>();

Enumerable.Range(0, 10).ToList().ForEach(p => list.Add("S1-00-" + (p + 220).ToString().PadLeft(4, '0')));
Enumerable.Range(0, 10).ToList().ForEach(p => list.Add("S1-00-" + p.ToString().PadLeft(4, '0')));
Enumerable.Range(0, 10).ToList().ForEach(p => list.Add("S1-00-" + (p+100).ToString().PadLeft(4, '0')));
Enumerable.Range(0, 10).ToList().ForEach(p => list.Add("S1-00-" + (p + 380).ToString().PadLeft(4, '0')));





然后我们需要用我们的方法对列表进行排序;





Then we need to sort list with our method;

list.Sort((a, b) => ExtractNumber(a).CompareTo(ExtractNumber(b)));

//Here's the result
var result = GetGroups(list).ToList();





方法;





Methods;

private int ExtractNumber(string text)
{
    var asd = new string(text.Reverse().TakeWhile(char.IsDigit).Reverse().ToArray());
    int result = 0;
    int.TryParse(asd, out result);
    return result;
}

private IEnumerable<IEnumerable><string>> GetGroups(List<string> list)
{
    while (list.Any())
    {
        if (list.Count == 1)
        {
            var single = list;
            list = list.Except(single).ToList();
            yield return single;
        }
        else
        {
            var sequentialList = list.TakeWhile((a, b) => ExtractNumber(list[b + 1 >= list.Count ? b : b + 1]) - ExtractNumber(list[b]) == 1).ToList();
            list = list.Except(sequentialList).ToList();
            sequentialList = sequentialList.Concat(list.Take(1)).ToList();
            list.RemoveAt(0);

            yield return sequentialList;
        }
    }
}





如果您发现任何错误,请告诉我。



If you notice any wrongs, please tell me.


嗨伙计们,

i认为我已经用这种方式解决了我的问题:



hi guys,
i think i've solved my problem this way:

List<object> series = new List<object>();
series = FindSeries(consecutive);





连续是带有第一个例子的项目的列表



的方法:





consecutive is the list with items like the first example

with the method:

public List<object> FindSeries(List<object> list)
        {
            List<object> series = new List<object>();
            //first
            series.Add(list[0]);

            for(int i = 1; i < list.Count; i++)
            {
                var checkNext = list[i].ToString().Split('-');
                var checkPrev = list[i - 1].ToString().Split('-');

                int valueOfNext = Convert.ToInt32(checkNext[2]);
                int valueOfPrev = Convert.ToInt32(checkPrev[2]);

                if(valueOfNext - 1 != valueOfPrev)
                {
                    series.Add(list[i - 1]);
                    series.Add(list[i]);
                }
            }

            //last
            series.Add(list[list.Count - 1]);

            return series;
        }





现在可行,我会尝试改进解决方案



for now it works, I'll try to improve the solution


这篇关于如何在List&lt; string&gt;中对字符串类型的项进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆