嵌套字典的递归

本文关键字:递归 字典 嵌套 | 更新日期: 2023-09-27 17:56:58

我有一个带有倍数值的字典。我的字典是:

public class checkitems: Dictionary<int, checkitems>{
  // ....
}

我像这样循环每个级别。

foreach(KeyValuePair<int, checkitems> first in items){
   foreach(KeyValuePair<int, checkitems> second in first.values){
       foreach(KeyValuePair<int, checkitems> third in second.values){
          List<int> list1 = new List<int>(); 
          if(third.Value.Age > 20){ 
              list.Add(x.key)
           }
        }
        foreach(var deleteKeys in list1){
        second.Value.Remove(deleteKeys)
        }
    }
}

目前,我正在为每个级别编写foreach,并检查它是否满足条件,然后将其添加到要删除的列表中。我想知道我将如何递归地编写它,这样我就不必担心关卡有多深。

Example data format:
    Companies 
         i. Apple
            a. key: Macbookpro   Value: 200
            b (key): IMAC        Value: 334
            c (key): Iphone      Value : 12
                1. (key) IOS8    Value : 13
                2. (key) IOS7    Value : 15
            d (key): Beats       Value: 20

嵌套字典的递归

我尽了最大的努力来理解你的代码想要实现的目标。希望这有帮助

using System;
using System.Collections.Generic;
namespace ConsoleApplication2
{
    public static class Program
    {
        public static void RecurseCheckitems(CheckItems items)
        {
            List<Int32> l_deleteKeys = new List<Int32>();
            // Step 1: DFS - Down down down to the deepest level
            foreach (Int32 key in items.Keys)
            {
                RecurseCheckitems(items[key]);
            }
            // Step 2: Extract all KEYS of Childelements having an AGE of at least 20
            foreach (Int32 key in items.Keys)
            {
                l_deleteKeys.AddRange(DoCheckItems(items[key]));
            }
            // Step 3: Remove all extracted keys from the current Objecct
            foreach (Int32 deleteKey in l_deleteKeys)
            {
                items.Remove(deleteKey);
            }
        }
        /// <summary>
        /// Helper-Function to extract the keys of Child-Elements having an age of at least 20
        /// </summary>
        /// <param name="item">Parent-Item to check</param>
        /// <returns>List of KEYS of Child-Elements having an Age of at least 20</returns>
        private static List<Int32> DoCheckItems(CheckItems item)
        {
            List<Int32> l = new List<Int32>();
            foreach (Int32 key in item.Keys)
            {
                if (item[key].Age > 20)
                {
                    l.Add(key);
                }
            }
            return l;
        }
    }
    public sealed class CheckItems : Dictionary<Int32, CheckItems>
    {
        public Int32 Age { get; set; }
    }
}

给定checkitemsroot实例,请尝试以下操作:

Func<checkitems, IEnumerable<Tuple<checkitems, checkitems>>> flatten = null;
flatten = cis =>
    cis
        .Select(x => Tuple.Create(cis, x.Value))
        .Concat(cis.SelectMany(c => flatten(c.Value)));
foreach (var tuple in flatten(root)
    .Where(x => x.Item2.Age > 20)
    .ToArray())
{
    tuple.Item1.Remove(tuple.Item2);
}