'用于语句'没有结肠
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))
上面,在“范围内的i”(3)中,应该有一个结尾“ :”,
但该行没有放置”: br> res = {test_keys [i]:test_values [i] for range(len(test_keys))}
这完全是我知道的语法, 这是怎么可能的?
也许是字典的语法?
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))
above, there should be an ending colon " : " after 'for statement' like for i in range(3) :
but this line didn't put " : " at the end of range()res = {test_keys[i]: test_values[i] for i in range(len(test_keys))}
This is totally out of syntax i knew,
how this is possible?
perhaps is it syntax for dictionary only?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用集合,词典,列表和生成器进行操作,并分别称为集合,字典和列表理解或生成器表达式:
输出:
You can do it with sets, dictionaries, lists and generators and is called set, dictionary and list comprehension respectively or generator expression:
Output:
在Python中,
:
用于指示这一行我们输入一个新的代码集团(更深的缩进级别启动下一行)。例如:
在列表中,理解所有内容都在同一行上,因此
:
不需要,并且会引起问题,因为下一行没有更深的缩进。In python, the
:
is used to indicate that this line we enter a new bloc of code (deeper level of indentation starting next line).Ex:
In list comprehension everything is on the same line so the
:
is not required and will cause issue since the next line don't have deeper indentation.您所描述的称为“词典理解”,这是创建迭代的替代语法。
它经常有用。
类似于列表理解:
What you have described is called "dictionary comprehension" and it's an alternative syntax for creating an iterable.
It comes in useful quite often.
Similar to list comprehension: