dict- Python:
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.
(Now I want C # to have less elementary support for ad-hoc data structures, I skip assignments for destructuring.)
An alternative version that does not require Zip () and thus will work with older versions of .NET, and avoids highlighting 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));
}
source
share