Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List - Fatal编程技术网

Python 对照可能值列表检查值

Python 对照可能值列表检查值,python,list,Python,List,我需要检查列表长度的东西。可能是这样,但这不起作用: if len(list) != 1 or 2: # Do something if len(list) == 1 or 2: # Do something different 好吧,我自己想出来了: if len(list) == 1: # Do something elif len(list) == 2: # Do the same something if len(list) != 2: # Do so

我需要检查列表长度的东西。可能是这样,但这不起作用:

if len(list) != 1 or 2:
   # Do something

if len(list) == 1 or 2:
   # Do something different
好吧,我自己想出来了:

if len(list) == 1:
   # Do something
elif len(list) == 2:
   # Do the same something

if len(list) != 2:
   # Do something different
elif len(list) != 1:
   # Do something different
差不多

if 1 <= len(list) <= 2:
    ...

如果有用的话

我会很快试试这个谢谢你的快速回复!看起来你的第二个建议是返回True或False…这两个都是返回True或False我只是想通过列表中元素的数量来获得列表的长度,你可以调用
len(myList)
。您还应该避免使用“list”作为列表的标识符,因为它是为Python中的list命令保留的?如果它等于1、2、5或6,你会做什么?不,只是一个数字
if len(list) in (1, 2):
    ...