开发者

Looking for a slicker way to convert delimited string to StringDictionary

开发者 https://www.devze.com 2023-04-12 03:46 出处:网络
I have created the following to expose some data as a regex match string as well as a StringDictionary. It feels like I could do this using LINQ with fewer lines.

I have created the following to expose some data as a regex match string as well as a StringDictionary. It feels like I could do this using LINQ with fewer lines.

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();
      va开发者_开发问答r streetArray = STREETTYPES.Split(PIPE);
      for (int i = 0; i < streetArray.Length-1; i = i+2)
      {
        streetTypes.Add(streetArray[i], streetArray[i + 1]);
      }
      return streetTypes;
    }
  }


How about just:

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"},
    ...        
};


You could use Linq if you need to recompute - but if the dictionary is static just use the initializer:

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]);


This any good?

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

Might be possible to compact it further but I'm half asleep tonight.


This will give you the StringDictionary you seek without doing positional math. If you can use a Dictionary, I'd go with it and one of the other posted solutions.

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);
    }
}


Shamelessly ripping off a Python dict constructor:

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);

This will produce a Dictionary<string, string>, not a StringDictionary. The latter doesn't have any copy constructors, so it can't be created with a oneliner.

(Now I'm wishing C# had less rudimentary support for ad-hoc data structures, I miss destructuring assignments.)

Alternative version that doesn't require Zip() and will thus work on older versions of .NET, and avoids allocating a list of temporary key/value pairs:

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));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号