本文介绍了locals()['_ [1]']在Python中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看到了一个据称可以从从序列中删除重复项的衬里代码:
I saw a one liner code that is claimed to remove duplicates from a sequence:
u = [x for x in seq if x not in locals()['_[1]']]
我在ipython(使用python 2.7)中尝试了该代码,它给出了KeyError: '_[1]'
I tried that code in ipython (with Python 2.7), it gave KeyError: '_[1]'
['_[1]']
在Python中是否有特殊含义?
Does ['_[1]']
mean something special in Python?
推荐答案
locals()['_[1]']
是一种访问对列表理解(或生成器)当前结果的引用的方法.
locals()['_[1]']
is a way to access a reference to list comprehension (or generator) current result inside that list comprehension.
这是很邪恶的,但是会产生有趣的结果:
It is quite an evil, but can produce funny results:
>> [list(locals()['_[1]']) for x in range(3)]
[[], [[]], [[], [[]]]]
在此处查看更多详细信息: the-secret-列表名称.
See more details here: the-secret-name-of-list-comprehensions.
这篇关于locals()['_ [1]']在Python中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!