Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查Python中是否存在列表_Python - Fatal编程技术网

如何检查Python中是否存在列表

如何检查Python中是否存在列表,python,Python,检查python中是否存在列表或dict的最简单方法是什么 我正在使用以下命令,但这不起作用: if len(list) == 0: print "Im not here" 谢谢,如果你能给它命名-它显然是“存在的”-我想你的意思是检查它是否是“非空的”。。。最具python风格的方法是使用if varname:。注意,这对生成器/iterables不起作用,无法检查它们是否返回数据,因为结果总是True 如果您只想使用某个索引/键,请尝试使用它: try: print som

检查python中是否存在列表或dict的最简单方法是什么

我正在使用以下命令,但这不起作用:

if len(list) == 0:
    print "Im not here"

谢谢,

如果你能给它命名-它显然是“存在的”-我想你的意思是检查它是否是“非空的”。。。最具python风格的方法是使用
if varname:
。注意,这对生成器/iterables不起作用,无法检查它们是否返回数据,因为结果总是
True

如果您只想使用某个索引/键,请尝试使用它:

try:
    print someobj[5]
except (KeyError, IndexError) as e: # For dict, list|tuple
    print 'could not get it'

您可以使用try/except块:

try:
    #work with list
except NameError:
    print "list isn't defined"
有关名单:

if a_list:
    print "I'm not here"
这同样适用于格言:

if a_dict:
    print "I'm not here"

当您尝试引用一个不存在的变量时,解释器会引发。但是,依赖代码中变量的存在是不安全的(最好将其初始化为“无”或其他)。有时候我用这个:

try:
    mylist
    print "I'm here"
except NameError:
    print "I'm not here"
示例:

mylist=[1,2,3]
'mylist' in locals().keys()
或者使用以下命令:

mylist in locals().values()
from types import ListType, TupleType
try:
    # for an existing/nonexisting list
    if type(ima_list) is ListType: 
        print "I'm a list, therefore I am an existing list! :)"

    # for an existing/nonexisting tuple
    if type(ima_tuple) is TupleType: 
        print "I'm a tuple, therefore I am an existing tuple! :)"
except Exception, e:
    print "%s" % e

Output:
    name 'ima_list' is not defined
    ---
    name 'ima_tuple' is not defined
简单控制台测试:

>>> if len(b) == 0: print "Ups!"
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
示例显示了如何检查列表是否包含元素:

alist = [1,2,3]
if alist: print "I'm here!"

Output: I'm here!
否则:

alist = []
if not alist: print "Somebody here?"

Output: Somebody here?

如果需要检查列表/元组的存在/不存在,这可能会有所帮助:

from types import ListType, TupleType
a_list = [1,2,3,4]
a_tuple = (1,2,3,4)

# for an existing/nonexisting list
# "a_list" in globals() check if "a_list" is defined (not undefined :p)

if "a_list" in globals() and type(a_list) is ListType: 
    print "I'm a list, therefore I am an existing list! :)"

# for an existing/nonexisting tuple
if "a_tuple" in globals() and type(a_tuple) is TupleType: 
    print "I'm a tuple, therefore I am an existing tuple! :)"
如果我们需要避免在globals()中使用也许我们可以使用:

mylist in locals().values()
from types import ListType, TupleType
try:
    # for an existing/nonexisting list
    if type(ima_list) is ListType: 
        print "I'm a list, therefore I am an existing list! :)"

    # for an existing/nonexisting tuple
    if type(ima_tuple) is TupleType: 
        print "I'm a tuple, therefore I am an existing tuple! :)"
except Exception, e:
    print "%s" % e

Output:
    name 'ima_list' is not defined
    ---
    name 'ima_tuple' is not defined
参考书目: 8.15. 类型-内置类型的名称-Python v2.7.3文档

检查(1)变量是否存在,以及(2)检查是否存在列表


你可以在这里找到更多信息:

除非OP澄清他的问题,否则我会同意这个观点。注意//是整数除法。使用#作为注释或更简单地说:
如果列表中的索引
@nicolas如果您只是想执行成员资格测试,那么这很好,但是我只是指出EAFP超过LBYLIf如果范围中没有您创建的名为
列表
的对象,此代码将尝试获取内置
列表的长度。这将导致异常
TypeError
list
dict
是内置类型。此代码将始终成功,即
if
语句中的条件。@mhawke、a_-list=list()、bool(a_-list)->False,与dict相同,“if语句”不区分False。如果一个列表,一个目录没有定义,那么我们将有一个奇妙的例外@VitalieGhelbert请查看问题的修订历史记录-在我发表评论后,该问题已被编辑。@mhawke,ok,tx u.:)什么?你的意思是
如果不是列表:打印“我不在这里”
??
s = set([1, 2, 3, 4])
if 3 in s:
    print("The number 3 is in the list.")
else:
    print("The number 3 is NOT in the list.")