F#: Iterating over a dictionary just returns itself?

こ雲淡風輕ζ 提交于 2020-01-06 14:35:07

问题


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

gives me

1
[(1, 2), (3, 4)]

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.)?

Secondly, is there a good way to iterate over and generally manipulate the key-value pairs in the dictionary, like in Python?


回答1:


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 ...

which uses the KeyValue active pattern.



来源:https://stackoverflow.com/questions/7699346/f-iterating-over-a-dictionary-just-returns-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!