【发布时间】:2022-10-20 08:43:10
【问题描述】:
test_keys = ["Rash", "Kil", "Varsha"]
test_values = [1, 4, 5]
# using dictionary comprehension
# to convert lists to dictionary
res = {test_keys[i]: test_values[i] for i in range(len(test_keys))}
# Printing resultant dictionary
print ("Resultant dictionary is : " + str(res))
上面,应该有一个结束冒号“:" 在 'for 语句' 之后,例如 for i in range(3) :
但是这一行没有把“:”放在 range() 的末尾res = {test_keys[i]: test_values[i] for i in range(len(test_keys))}
这完全不符合我所知道的语法,
这怎么可能?
也许它只是字典的语法?
【问题讨论】:
-
冒号
:表示以下代码块引用包含该冒号的行。但是,在理解中,您不需要冒号。