简体   繁体   English

如何在 for 循环中计算值作为字典键的一部分

[英]How do I counter value in a for loop as part of my dictionary key

I am trying to create a for loop that adds keys and values to a dictionary every time it loops.我正在尝试创建一个 for 循环,每次循环时将键和值添加到字典中。 The counter value of this for loop is part of the key name that gets added to the dictionary every time.这个 for 循环的计数器值是每次添加到字典中的键名的一部分。 How can I code this without defining a separate set for key values and assigning them to key values?如何在不为键值定义单独的集合并将它们分配给键值的情况下对此进行编码? or better said, how can I remove this line?或者更好地说,我怎样才能删除这条线? "y.insert(i, "This is key number " + str(i+1))" "y.insert(i, "这是关键号码" + str(i+1))"

Here is my current code:这是我当前的代码:

Dic = {}
y = []
for i in range(0,4):
    y.insert(i, "This is key number " + str(i+1))
    Dic[y[i]] = "This is a constant value for all keys"

You literally just insert the new items at the last position of the list (for which you should use append , but that's besides the point), just to then get the element at that index out of the list.您实际上只是在列表的最后一个位置insert新项目(为此您应该使用append ,但这不是重点),然后从列表中获取该索引处的元素。 Instead, you can just assign it to a temporary variable, without the list:相反,您可以将它分配给一个临时变量,而不需要列表:

Dic = {}
for i in range(0,4):
    x = "This is key number " + str(i+1)
    Dic[x] = "This is a constant value for all keys"

Of course, you don't really need that variable either, you can just put the expression into the [...] (but it might be argued that the above is more readable):当然,您也不需要该变量,您可以将表达式放入[...] (但可能会认为上述内容更具可读性):

Dic = {}
for i in range(0,4):
    Dic["This is key number " + str(i+1)] = "This is a constant value for all keys"

Which now can be directly translated to a dictionary comprehension:现在可以直接翻译成字典理解:

Dic = {"This is key number " + str(i+1): "This is a constant value for all keys" for i in range(0,4)}

After a bit of cleanup (note: f-strings require newer versions of Python, but there are other ways to format the number into the string without concatenation):经过一些清理(注意:f-strings 需要更新版本的 Python,但还有其他方法可以将数字格式化为不连接的字符串):

dic = {f"This is key number {i+1}": "This is a constant value for all keys" for i in range(4)}

If you want to stick with something close to your starting for loop, just do:如果你想坚持一些接近你开始的 for 循环,只需执行以下操作:

Dic = {}
for i in range(0,4):
    Dic[f"This is key number {i}"] = "This is a constant value for all keys"

You can use the dict's fromkeys() method:您可以使用 dict 的 fromkeys() 方法:

Dic = dict.fromkeys(map("This is key number {}".format,range(4)),"constant")

print(Dic)
{'This is key number 0': 'constant', 
 'This is key number 1': 'constant', 
 'This is key number 2': 'constant', 
 'This is key number 3': 'constant'}

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

相关问题 元组是计数器中的字典键 - 我如何使它成为一个字符串? - Tuple is a dictionary key in counter - how do I make it a string? 我如何将字典 append 作为现有字典键的另一个值 - How do I append a dictionary as another value to an existing dictionary key Python3:我如何 append 多个键、值对到 for 循环中的字典? - Python3: How do I append multiple key,value pairs to a dictionary in for loop? 如何使用 for 循环将键和值添加到字典中 - how do you add a key and value to a dictionary with a for loop 如何判断字典中特定键的值是否是字典,以便我可以基于此递归解决编程问题? - How do I tell if the value of a particular key in my dictionary is a dictionary so that I can solve programming questions based on this recursively? 如何在字典关键字中添加for循环? - How do I add the for loop inside the dictionary key? 在字典中添加键之前,如何使用循环? - How do I use a loop until a key is added in a dictionary? 遇到相同的字典键值时如何增加计数器 - How to increase the counter when a same dictionary key value encountered 如何检查字典中是否存在键值对? 如果一个键的值是字典 - How do I check if a key-value pair is present in a dictionary? If the value of one key is a dictionary 如何从字典中的键中删除换行符? - How do I remove newline from my key in a dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM