【发布时间】:2016-04-22 02:40:23
【问题描述】:
我在 F# 中遇到了这个问题 [不是 C#,那里已经有类似的帖子有类似的答案]
我了解在 for 循环中枚举字典时无法修改字典 我应该如何解决这个问题?
let edgelist1 = [(1,2,3.0f);(1,2,4.0f);(5,6,7.0f);(5,6,8.0f)]
let dict_edges = new Dictionary<int*int,(int*int*float32) list>()
for x in edgelist1 do dict_edges.Add ((fun (a,b,c)-> (a,b)) x, x)
for k in dict_edges.Keys do dict_edges.[k] <- (dict_edges.[k] |> List.rev)
System.InvalidOperationException:集合已修改;枚举 操作可能无法执行。
在 System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource 资源)在 System.Collections.Generic.Dictionary`2.KeyCollection.Enumerator.MoveNext() 在 .$FSI_0101.main@()
个人认为这是有效的
dict_edges.[(1,2)] <- dict_edges.[(1,2)] |> List.rev;;
在 for 循环中,我只需要更改字典值,而不是键。
谢谢
【问题讨论】:
标签: list dictionary f#