寻找一种更灵活的方式将分隔的字符串转换为StringDictionary

时间:2011-10-11 22:19:14

标签: c#

我创建了以下内容,将一些数据公开为正则表达式匹配字符串以及StringDictionary。感觉就像我可以使用更少行的LINQ来做到这一点。

private const string STREETTYPES = @"ALY|Alley|AVE|Avenue|BLVD|Boulevard|CIR|Circle|CT|Court|CTR|Center|DR|Drive|EXPY|Expressway|FWY|Freeway|HALL|Hall|HWY|Highway|JCT|Junction|LN|Lane|LP|Loop|PIKE|Pike|PKWY|Parkway|PL|Place|RD|Road|ST|Street|TER|Terrace|TPKE|Turnpike|TRL|Trail|WAY|Way";

  private static StringDictionary streetTypes = null;
  public static StringDictionary StreetTypes
  {
    get
    {
      if (streetTypes != null) return streetTypes;
      streetTypes = new StringDictionary();
      var streetArray = STREETTYPES.Split(PIPE);
      for (int i = 0; i < streetArray.Length-1; i = i+2)
      {
        streetTypes.Add(streetArray[i], streetArray[i + 1]);
      }
      return streetTypes;
    }
  }

5 个答案:

答案 0 :(得分:4)

如何:

private static readonly StringDictionary streetTypes = new StringDictionary
{
    {"ALY","Alley"},{"AVE","Avenue"},{"ALY","Alley"},{"BLVD","Boulevard"},{"CIR","Circle"},
    {"CT","Court"},{"CTR","Center"},{"DR","Drive"},{"EXPY","Expressway"},{"FWY","Freeway"},
    {"HALL","Hall"},{"HWY","Highway"},{"JCT","Junction"},{"LN","Lane"},{"LP","Loop"},
    ...        
};

答案 1 :(得分:1)

这有什么好处吗?

var x = STREETTYPES.Split(new[] {'|'});
var output = Enumerable
    .Range(0, x.Length / 2)
    .ToDictionary(s => x[2 * s], s => x[2 * s + 1]);

可能会进一步压缩它,但我今晚半睡半醒。

答案 2 :(得分:1)

如果需要重新计算,可以使用Linq - 但如果字典是静态的,只需使用初始化程序:

var input = STREETTYPES.Split('|');
var dict = input.Select( (x,i) => new { Item = x, Index = i })
                .Where(x => x.Index % 2 == 0)
                .ToDictionary( x=> input[x.Index], x => input[x.Index + 1]);

答案 3 :(得分:0)

无耻地扯掉Python dict构造函数:

IDictionary<TKey, TVal> 
ToDictionary<TKey, TVal>(IEnumerable<TKey> keys, 
                         IEnumerable<TVal> values) 
{
    return keys.Zip(values, (k, v)=>new {K=k, V=v}).ToDictionary(kv=>kv.K, kv=>kv.V);
}

var str = @"ALY|Alley|AVE|Avenue|BLVD|Boulevard|CIR|Circle|CT|Court|CTR|Center|DR|Drive|EXPY|Expressway|FWY|Freeway|HALL|Hall|HWY|Highway|JCT|Junction|LN|Lane|LP|Loop|PIKE|Pike|PKWY|Parkway|PL|Place|RD|Road|ST|Street|TER|Terrace|TPKE|Turnpike|TRL|Trail|WAY|Way";
var words = str.Split('|');
var keys = words.Where((w, i) => i%2 == 0);
var values = words.Where((w, i) => i%2 != 0);
var dict = ToDictionary(keys, values);

这将生成Dictionary<string, string>,而不是StringDictionary。后者没有任何拷贝构造函数,因此无法使用oneliner创建。

(现在我希望C#对ad-hoc数据结构的初步支持不足,我错过了解构分配。)

替代版本不需要Zip(),因此可以在旧版本的.NET上运行,并避免分配临时键/值对列表:

IDictionary<TKey, TVal> 
ToDictionary<TKey, TVal>(IEnumerable<TKey> keys, 
                         IEnumerable<TVal> values) 
{
    return Enumerable.Range(0, keys.Count()).ToDictionary(i=>keys.ElementAt(i), i=>values.ElementAt(i));
}

答案 4 :(得分:0)

这将为您提供所需的StringDictionary而无需进行位置数学运算。如果您可以使用Dictionary,我会使用它和其他一个发布的解决方案。

var types = new StringDictionary();
using (IEnumerator<string> enumerator = streetTypes.Split('|').AsEnumerable().GetEnumerator())
{
    while(enumerator.MoveNext())
    {
        string first = enumerator.Current;
        if (!enumerator.MoveNext()) 
            break;
        types.Add(first, enumerator.Current);
    }
}