问题描述
我正在尝试编写一个简单的函数来查找列表中是否按该顺序出现 0,1。
它应该返回 True
或 False
。
列表可以包含任意数量的数字。
对于函数 ZeroZeroOne
示例如下:
>> ZeroZeroOne( [0,1] )
>> True
>> ZeroZeroOne( [1,0] )
>> False
# there are 2s in between but the following does have 0,1 occurring and in correct order
>> ZeroZeroOne( [0,2,1] )
>> True
我有这个功能:
def ZeroZeroOne(nums):
FoundIt = False
#quick return if defo not possible
if (nums.count(0) < 2) and (nums.count(1) == 0):
return FoundIt
n = len(nums)
for x in range(n-2):
if nums[x] == 0:
for i,z in enumerate(nums[(x+1):]):
if z==0 and z!=1:
for j,q in enumerate(nums[(i+1):]):
if q==1 and q!=0:
FoundIt=True
return FoundIt
为什么函数为这个列表 [0,1,1]
返回 True?
此外....
对于一个看似简单的问题,这个函数似乎过于复杂。
- 在 Python 中是否有解决此问题的正确方法 - canonical 或 Pythonic 方法?
- 或者一个方法只是基于意见?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)