【问题标题】:how to count conditioned continuous values in a list with linq? (Revized)如何使用 linq 计算列表中的条件连续值? (修订)
【发布时间】:2014-05-16 06:03:14
【问题描述】:

我前段时间问过一个问题 -> how to count conditioned continuous values in a list with linq?

@Fung 回答了这个问题。但是,我改变了问题的一些规则。新条件:

var query = Enumerable.Range(0, 1440).Select((n, index) =>
    {
        if ((index >= 480 && index <= 749) || (index >= 810 && index <= 999) || (index >= 1080 && index <= 1099) || (index>=1200 && index <= 1299))
            return 0;
        else if ((index >= 750 && index <= 809) || (index >= 1100 && index <= 1199))
            return 1;
        else
            return 2;
    });

那么,我能否找到有多少索引连续具有“0”值,哪些是它们的索引 - 即使被“1”(不是 2)中断 - ?例如;

query[480]=query[481]=query[482]....query[749] = 0, 
query[750]=query[751]...query[809] = 1, 
query[810]=query[811]....query[999] = 0, 
query[1000]?query[1001]...query[1079] = 2, 
query[1080]=query[1081]....query[1099] = 0, 
query[1100]=query[1101]....query[1199] = 1,
query[1200]=query[1201]....query[1299] = 0, etc..

所以,答案是 270(1 之前)+ 190(1 之后)= 460 尽管 1080 到 1099 之间的索引为 0,但不应考虑它们,因为之前的值为“2”。

此外,应考虑从 1080 到 1099 和从 1200 到 1299 的值,因为它们还有新的 0 - 1 - 0 系列。而且,这个系列应该是另一个列表。有没有办法做到这一点?

我现在有这些结果:

但是,由于它除以“2”,我希望它们在另一个列表中:

【问题讨论】:

    标签: c# linq list indexing


    【解决方案1】:
    public class ListStuff
    {
        public int Count { get; set; }
        public int Start { get; set; }
        public int End { get; set; }
        public int LType { get; set; }
        public int Group { get; set; }
    }
    
    
    var query = Enumerable.Range(0, 1440).Select((n, index) =>
                {
                    if ((index >= 480 && index <= 749) || (index >= 810 && index <= 999) || (index >= 1080 && index <= 1099) || (index >= 1200 && index <= 1299))
                        return 0;
                    else if ((index >= 750 && index <= 809) || (index >= 1100 && index <= 1199))
                        return 1;
                    else
                        return 2;
                });
    
    
            int typeCount = 0;
            int groupCount = 0;
            var result = query
                   .Select((x, i) => new
                   {
                       Value = x,
                       Index = i,
                       Group = (((i == 0 || (query.ElementAt(i - 1) == 2)) && x != 2) ? ++groupCount : groupCount)
                   })
                   .GroupBy(x =>
                       x.Index == 0 || x.Value != query.ElementAt(x.Index - 1)
                           ? ++typeCount
                           : typeCount)
                   .Select(x => new ListStuff()
                   {
                       Count = x.Count(),
                       Start = x.First().Index,
                       End = x.Last().Index,
                       LType = x.First().Value,
                       Group = x.First().Group
                   })
                   .Where(x => x.LType == 0 || x.LType == 1)
                   .GroupBy(x => x.Group)
                   .ToList();
    

    【讨论】:

    • 不幸的是,它不是。
    猜你喜欢
    • 2014-04-16
    • 2014-02-24
    • 2019-08-22
    • 1970-01-01
    • 2017-02-26
    • 2020-10-19
    • 1970-01-01
    • 2021-10-08
    • 2017-03-14
    相关资源
    最近更新 更多