F#:迭代字典只是返回自己?

Li *_*oyi 0 collections f# dictionary loops

let h = dict [(1, 2), (3, 4)]
Console.WriteLine(h.Count)
for i in h do
    Console.WriteLine(i)
Run Code Online (Sandbox Code Playgroud)

给我

1
[(1, 2), (3, 4)]
Run Code Online (Sandbox Code Playgroud)

两个问题.首先,为什么迭代dict会给我一个只有1个项目的序列,dict本身?这背后可能存在一些逻辑,这也会影响我最终试图迭代的其他事情.这对于由dict(Any(),All(),Aggregate()等公开的所有其他Seq成员意味着什么?)?

其次,是否有一种很好的方法来迭代并通常操纵字典中的键值对,就像在Python中一样?

Bri*_*ian 11

你使用逗号,你需要一个分号.

[(1,2); (3,4)]
Run Code Online (Sandbox Code Playgroud)

元组的逗号,列表元素的分号.(这里的parens是可选的.)

如果要迭代字典中的键值对,可以使用

for KeyValue(k,v) in someDictionary do ...
Run Code Online (Sandbox Code Playgroud)

它使用KeyValue活动模式.