【问题标题】:Get a specific string form a list of strings that mathces a tricky criteria从符合棘手条件的字符串列表中获取特定字符串
【发布时间】:2022-11-13 22:22:48
【问题描述】:

我有一个对象,它有一个字符串列表,其中每个字符串代表一个区域(NUTS 代码)。 前任。

["SE","SE12","SE124"]

我要做的是获得最一般和最具体的一个(我不知道我是否有道理)我会写一些输入示例以及预期的输出是什么,以便更清楚我意思是。

input1 : ["SE", "SE123", "SE124", "SE123456", "SE123456789"],
input2 : ["SE", "SE2", "SE123", "SE123456", "SE123456789"],
input3 : ["SE", "SE123", "SE123456", "SE123456789"],
input4 : ["SE","FI", "SE2"]

预期的输出应该是:输出1 =>“SE12”,输出2 =>“SE”,ouptut3 =>“SE123456789”,输出=>“”。

我使用了不同的方法,但它似乎比我想象的要棘手。

我的方法目前看起来像这样:

 public static string GetSpecificNuts(IList<string> nuts)
{
    var outNuts = "";
    var annNuts = nuts.Distinct().ToList();
    if (annNuts.Any())
    {
        if (annNuts.Count() == 1)
        {
            outNuts = annNuts.SingleOrDefault();
        }
        else
        {
            var grouped = annNuts.GroupBy(n => n.Length).OrderByDescending(n=>n.Key).ToList();
            var highest = grouped.Select(g => g.Key).FirstOrDefault();

            var highestGroup = grouped?.SingleOrDefault(g => g.Key == highest)?.ToList();
            var length = highestGroup?.Count;

            if (length == 1)
            {
                var highestNuts = highestGroup?.SingleOrDefault();
                var contained = grouped?.Where(n => n.Key != highest).SelectMany(g => g.ToList()).Where(s => highestNuts.StartsWith(s)).OrderByDescending(s=>s.Length);
                var firstContained = contained.FirstOrDefault();
                if (!string.IsNullOrWhiteSpace(firstContained))
                {
                    outNuts = firstContained;
                }
            }
            while (length > 1)
            {
                var deducted = new List<string>();
                highestGroup?.ForEach(i => { deducted.Add(i.Length > 2 ? i.Remove(i.Length - 1, 1) : i); });
                var distinct = deducted?.Distinct().ToList();
                length = distinct?.Count;
                highestGroup = distinct;
                if (length == 1)
                {
                    outNuts = distinct?.SingleOrDefault();
                }
            }
        }
    }

    return outNuts;
}

有什么想法吗?

编辑更多解释: 将前 2 个字母之后的数字视为树视图。第一个数字代表一组州,第二个代表一个州,第三个代表一个区,第四个代表自治市..等等。 我需要获得最具体的区域,我在 input3 中实现了这一点。 但如果列表有前。 2个或更多不同的地区然后我需要获得代表该州的数字。再有 2 个不同的状态,然后我需要获得代表状态组的数字。 2个或更多不同的州组然后我需要得到代表国家的前2个字母。 2 或国家代码 ex ("SE","FI") 那么输出应该是一个空字符串。

【问题讨论】:

  • 预期产出的标准是什么?你能用英文提供吗
  • 我对这个问题添加了更多解释我希望它能让它更清楚

标签: c# .net string linq


【解决方案1】:

该方法的命名有点困难,但这样的事情应该可以工作:

public static string GetDeepestNodeInCommonBranch(IList<string> nutsCodes)
{
    // Build a tree with the depth being the specificity (index in the code) of
    // the country subdivisions and the nodes at each level being the different
    // divisions at that level/specificity (different numbers at the same index).
    
    var levels = nutsCodes.Distinct(StringComparer.OrdinalIgnoreCase)
        .SelectMany(nc => nc.Select((c, i) => new {Character = c, Index = i}))
        .GroupBy(obj => obj.Index)
        // Walk the tree from the top
        .OrderBy(g => g.Key);
    
    var commonDiv = string.Empty;
    foreach (var level in levels)
    {
        var divisions = level.Select(obj => obj.Character).Distinct().ToList();
        // If the tree branches out here, the division at the last level is the
        // most specific division common for all
        if (divisions.Count > 1)
        {
            // Only return full country codes
            return commonDiv.Length >= 2 ? commonDiv : string.Empty;
        }
        commonDiv += divisions.Single();
    }
    return commonDiv;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 2015-11-23
    • 2012-01-12
    相关资源
    最近更新 更多