Python: "as"关键字简写

标签 python iteration shorthand

简单的问题:在 Python 中迭代时是否可以分配速记引用,如下所示(这不起作用):

class SomeClass:
    class_dict = {0:0,1:1,2:2,3:3}

newClass = SomeClass()

for n in newClass.class_dict as thedict: # assigning a shorthand reference "thedict" to newClass.class_dict
    print(thedict[n])

最佳答案

我想你想要这个(使用iteritems()将键/值对迭代为n, o):

class SomeClass:
    class_dict = {0:0,1:1,2:2,3:3}

newClass = SomeClass()

for n, o in newClass.class_dict.iteritems():
    print(o)

关于Python: "as"关键字简写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17416264/

相关文章:

java - java8 Stream的filter()和map()方法是否使用迭代?

javascript - 如果没有 "function"关键字,这个对象方法定义如何工作?

go - 速记返回

background - css3 多重背景 - 渐变和图像的简写

Python:如果找不到文件,请尝试查找不同的文件名

python - 更新整数的值(我认为)

c++ - 迭代无符号整数的所有值的最漂亮方法

python - 加速高斯 EM 算法

python - Ctrl+C 后 Pycharm 控制台崩溃

swift - 使用条件排序遍历通用类型的 Swift 字典