F#:迭代一个字典只返回自己? [英] F#: Iterating over a dictionary just returns itself?
问题描述
let h = dict [(1, 2), (3, 4)]
Console.WriteLine(h.Count)
for i in h do
Console.WriteLine(i)
给我
1
[(1, 2), (3, 4)]
两个问题。首先,为什么迭代一个字典给我一个序列只有1项,字典本身?在这之后可能有一些逻辑,这也将影响我最终试图迭代的其他事情。这对于由dict(Any(),All(),Aggregate()等)暴露的所有其他Seq成员意味着什么?
Two questions. Firstly, why does iterating over a dict give me back a sequence which only has 1 item, the dict itself? There is probably some logic behind this that will also affect other things I end up trying to iterate over. What does this mean for all the other Seq members exposed by dict (Any(), All(), Aggregate(), etc.)?
其次,好的方法来迭代和通常操纵字典中的键值对,就像在Python?
Secondly, is there a good way to iterate over and generally manipulate the key-value pairs in the dictionary, like in Python?
推荐答案
您在需要分号的地方使用了逗号。
You used a comma where you need a semicolon.
[(1,2); (3,4)]
元组的逗号,列表元素的分号。 (这里的括号是可选的。)
Commas for tuples, semicolons for list elements. (The parens here are optional.)
如果你想迭代字典中的键值对,可以使用
If you want to iterate over key-value pairs in the dictionary, you can use
for KeyValue(k,v) in someDictionary do ...
b $ b
使用 KeyValue 活动模式。
这篇关于F#:迭代一个字典只返回自己?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!