MSDN解释了这样的Lookup:
一个
Lookup<TKey, TElement>酷似Dictionary<TKey, TValue>.不同之处在于 Dictionary <TKey,TValue>将键映射到单个值,而 Lookup <TKey,TElement>将键映射到值集合.
我没有发现这种解释特别有用.Lookup用于什么?
我想出了我在F#代码中需要的这个简单的算法(将元组列表转换为列表键的映射集合):
let MergeIntoMap<'K,'V when 'K: comparison>(from: seq<'K*'V>): Map<'K,seq<'V>>=
let keys = from.Select(fun (k,v) -> k)
let keyValuePairs = seq {
for key in keys do
let valsForKey = from.Where(fun (k,v) -> key = k).Select(fun (k,v) -> v) |> seq
yield key,valsForKey
}
keyValuePairs |> Map.ofSeq
Run Code Online (Sandbox Code Playgroud)
输入示例:
[ ("a", 1); ("b", 2), ("a", 3) ]
Run Code Online (Sandbox Code Playgroud)
输出:
dict [ ("a", [1; 3]), ("b", [2]) ]
Run Code Online (Sandbox Code Playgroud)
而且我认为这必须是BCL或F#的高阶函数中的某些东西?如果是,有人可以引用我吗?因为我确定我的代码效率不高,因为它...