1

次のデータを含む辞書があります。

Key    Value
1      Introduction
1.1    General
1.1.1  Scope
1.2    Expectations
2      Background
2.1    Early Development
...

私がやりたいのは、次のように、リストスタイルのキーに基づいて連結された値で新しいリストを作成する (または配列に追加する) 方法を C# で見つけることです。

Key    Value              Concatenation
1      Introduction       Introduction
1.1    General            Introduction - General
1.1.1  Scope              Indroduction - General - Scope
1.2    Expectations       Introduction - Expectations
2      Background         Background
2.1    Early Development  Background - Early Development
...

キーのサブレベルの数は決まっていませんが、常に数値形式になります。何かご意見は?

4

4 に答える 4

1

これは明らかにクリーンアップしてより効率的にする必要がありますが、再帰的な方法を使用してこれを非常に簡単に行うことができます。

static string GetConcatenationRecursively(Dictionary<string, string> d, string key)
{
    if (key.Length == 1)
    {
        return d[key];
    }
    else
    {
        return string.Format(
            "{0} - {1}",
            GetConcatenationRecursively(d, key.Substring(0, key.LastIndexOf('.'))),
            d[key]);
    }
}

これは次のように呼ばれます:

Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("1", "Introduction");
d.Add("1.1", "General");
d.Add("1.1.1", "Scope");
d.Add("1.2", "Expectations");
d.Add("2", "Background");
d.Add("2.1", "Early Development");

List<Tuple<string, string, string>> list = new List<Tuple<string, string, string>>();
foreach (string key in d.Keys)
{
    list.Add(new Tuple<string, string, string>(key, d[key], GetConcatenationRecursively(d, key)));
}

同様に多くのエラー処理が必要です。これは明らかに整形式の入力を前提としています。しかし、あなたはここからそれを取ることができるはずです。

于 2012-11-21T19:37:08.703 に答える
0

このソリューションは、あなたが思いつくことができる最も美しいものではないかもしれませんが、要件が本当に単純であることがわかっている場合は、それを過度にエンジニアリングして、このアプローチのような単純なものを先に進めたくないかもしれません-注意してくださいほとんどのコードは、「 - 」部分を正しくレンダリングするためのボイラープレートにすぎません。実際のアルゴリズム部分はそれほど多くのコードではありません:

var dic = new Dictionary<string, string>();
      dic.Add("1", "Introduction");
      dic.Add("1.1", "General");
      dic.Add("1.1.1", "Scope");
      dic.Add("1.2", "Expectations");
      dic.Add("2", "Background");
      dic.Add("2.1", "Early Development");

      foreach (var kvp in dic)
      {
        string item = String.Empty;

        int length = kvp.Key.Length - 2;
        while (length > 0)
        {
          var parent = dic[kvp.Key.Substring(0, length)];
          if (!String.IsNullOrEmpty(item))
            item = String.Format("{0} - {1}", parent, item);
          else
            item = parent;
          length -= 2;
        }
        if (!String.IsNullOrEmpty(item))
          item = String.Format("{0} - {1}", item, kvp.Value);
        else
          item = kvp.Value;
        Console.WriteLine(item);
      }
于 2012-11-21T19:23:00.043 に答える
0

表示を出力するだけでよい場合は、Maateの回答のようなものを使用してください。コレクションでこれが必要な場合は、以下から始めます (ただし、1 レベル上にしか上がりません)。

Dictionary<string, string> d = new Dictionary<string, string>();
d.Add("1", "Introduction");
d.Add("1.1", "General");
d.Add("1.1.1", "Scope");
d.Add("1.2", "Expectations");
d.Add("2", "Background");
d.Add("2.1", "Early Development");

var links = from key in d.Keys
            from subKey in d.Keys
            where subKey.StartsWith(key) && (subKey.Length == key.Length + 2)
            select new
            {
                Key = key,
                SubKey = subKey
            };

var a = from key in d.Keys
        join link in links on key equals link.SubKey into lj
        from sublink in lj.DefaultIfEmpty()
        select new
        {
            Key = key,
            Value = d[key],
            Concatenation = (sublink == null ? string.Empty : d[sublink.Key] + " - ") + d[key]
        };

リンクを取得し、辞書からリンクへの左結合を使用して連結を取得します。私が言ったように、それは 1 レベルしか上がらないので、これは完全な解決策ではありません。無限再帰が必要です。

于 2012-11-21T19:27:44.770 に答える
-2

分割できる文字列に区切り文字を入れます。通常はセミコロン(;)です。しかし、もっと使うことができます。

セミコロンを使用する場合は、必ず文字列からセミコロンを削除してください。

于 2012-11-21T19:01:54.937 に答える