'用于语句'没有结肠

发布于 2025-01-24 14:44:52 字数 504 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

救赎№ 2025-01-31 14:44:52

您可以使用集合,词典,列表和生成器进行操作,并分别称为集合,字典和列表理解或生成器表达式:

set_comprehension = {i for i in range(10)}
dict_comprehension = {i:i for i in range(10)}
list_comprehension = [i for i in range(10)]
generator_expression = (i for i in range(10))

print(set_comprehension)
print(dict_comprehension)
print(list_comprehension)
print(generator_expression)

输出:

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
<generator object <genexpr> at 0x7fe9e8999dd0>

You can do it with sets, dictionaries, lists and generators and is called set, dictionary and list comprehension respectively or generator expression:

set_comprehension = {i for i in range(10)}
dict_comprehension = {i:i for i in range(10)}
list_comprehension = [i for i in range(10)]
generator_expression = (i for i in range(10))

print(set_comprehension)
print(dict_comprehension)
print(list_comprehension)
print(generator_expression)

Output:

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
<generator object <genexpr> at 0x7fe9e8999dd0>
泪痕残 2025-01-31 14:44:52

在Python中,用于指示这一行我们输入一个新的代码集团(更深的缩进级别启动下一行)。
例如:

if condition:
    pass #Deeper indentation
for i in range(10):
    pass #Deeper indentation
while True:
    pass #Deeper indentation
#And many others

在列表中,理解所有内容都在同一行上,因此不需要,并且会引起问题,因为下一行没有更深的缩进。

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:

if condition:
    pass #Deeper indentation
for i in range(10):
    pass #Deeper indentation
while True:
    pass #Deeper indentation
#And many others

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.

迷雾森÷林ヴ 2025-01-31 14:44:52

您所描述的称为“词典理解”,这是创建迭代的替代语法。

它经常有用。

类似于列表理解:

newlist = [expression for item in iterable if condition]

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:

newlist = [expression for item in iterable if condition]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文