简体   繁体   English

循环进入字典理解

[英]Loop inside dictionary comprehension

I'm curious if I can make a dict comprehension out of this dict (the frequency of some dice rolls): 我很好奇我是否可以根据此dict(一些掷骰子的频率)来理解dict:

freq_dict = {}
for i in range(2, max_result+1):
    freq_dict[i] = results.count(i)

I can get the right keys when trying to simplify this via comprehension but I cannot iterate through the values. 尝试通过理解简化此操作时,我可以获得正确的键,但是无法遍历值。

I'm very new to programming and if there is a better way to do this and dict comprehension is not the best one I would like to learn it! 我对编程非常陌生,如果有更好的方法可以做到这一点,而dict理解并不是我想学习的最好方法!

freq_dict = { i: results.count(i) for i in range(2, max_result+1) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM