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):
...