我看到一个声称为 remove duplicates from a sequence 的单行代码:
u = [x for x in seq if x not in locals()['_[1]']]
我在 ipython 中尝试了该代码(使用 Python 2.7),它给出了 KeyError: '_[1]'
['_[1]']
在 Python 中有什么特殊意义吗?
最佳答案
locals()['_[1]']
是一种访问对列表理解(或生成器)当前结果的引用的方法。
这很邪恶,但会产生有趣的结果:
>> [list(locals()['_[1]']) for x in range(3)]
[[], [[]], [[], [[]]]]
在此处查看更多详细信息:the-secret-name-of-list-comprehensions .
关于python - locals() ['_[1]' ] 在 Python 中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9631777/