Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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_Python 3.x_List - Fatal编程技术网

Python 有没有办法用“少”字来写;嵌套;?

Python 有没有办法用“少”字来写;嵌套;?,python,python-3.x,list,Python,Python 3.x,List,这个问题是一个家庭作业问题。我想我解决了,但我想知道这是最好的解决办法吗?问题是获取一个列表并遍历它,看看它是否包含一个特定的子序列:007。在这里,子序列用于数学意义上(因此0110227确实包含007并返回True) def spy_游戏(nums): 如果在nums中为0: a_1=nums.索引(0) nums_1=nums[a_1+1:] 如果nums_1中为0: a_2=nums_1.索引(0) 如果7单位为nums[a_2+1:]: 返回真值 其他: 返回错误 其他: 返回错误 其

这个问题是一个家庭作业问题。我想我解决了,但我想知道这是最好的解决办法吗?问题是获取一个列表并遍历它,看看它是否包含一个特定的子序列:007。在这里,子序列用于数学意义上(因此0110227确实包含007并返回True)

def spy_游戏(nums):
如果在nums中为0:
a_1=nums.索引(0)
nums_1=nums[a_1+1:]
如果nums_1中为0:
a_2=nums_1.索引(0)
如果7单位为nums[a_2+1:]:
返回真值
其他:
返回错误
其他:
返回错误
其他:
返回错误

从否定测试条件开始,这样您就可以提前返回。最后一个测试根本不需要
if
语句,因为此时条件的结果就是函数的返回值

def spy_game(nums):
    if 0 not in nums:
        return False

    a_1 = nums.index(0)
    nums_1 = nums[a_1+1:]

    if 0 not in nums_1:
        return False

    a_2 = nums_1.index(0)
    return 7 in nums[a_2+1:]

从否定你的测试条件开始,这样你就可以早点回来。最后一个测试根本不需要
if
语句,因为此时条件的结果就是函数的返回值

def spy_game(nums):
    if 0 not in nums:
        return False

    a_1 = nums.index(0)
    nums_1 = nums[a_1+1:]

    if 0 not in nums_1:
        return False

    a_2 = nums_1.index(0)
    return 7 in nums[a_2+1:]

如果否定包含条件,则可以提前返回,而不是引入进一步的嵌套

def spy_game(nums):
    if 0 not in nums:
        return False
    a_1 = nums.index(0)
    num_1 = nums[a_1+1:]
    if 0 not in nums_1:
        return False
    a_2 = nums_1.index(0)
    return 7 in nums_1[a_2+1:]
您也可以通过使用index方法的参数来编写它,而无需创建列表的任何副本,这将告诉index方法开始搜索元素的索引。这段代码看起来不同,但实现了相同的功能,可能更易于维护

def spy_game(nums):
    try:
        start = 0
        for n in [0, 0, 7]:
            start = nums.index(n, start)
        return True
    except ValueError:
        return False

如果否定包含条件,则可以提前返回,而不是引入进一步的嵌套

def spy_game(nums):
    if 0 not in nums:
        return False
    a_1 = nums.index(0)
    num_1 = nums[a_1+1:]
    if 0 not in nums_1:
        return False
    a_2 = nums_1.index(0)
    return 7 in nums_1[a_2+1:]
您也可以通过使用index方法的参数来编写它,而无需创建列表的任何副本,这将告诉index方法开始搜索元素的索引。这段代码看起来不同,但实现了相同的功能,可能更易于维护

def spy_game(nums):
    try:
        start = 0
        for n in [0, 0, 7]:
            start = nums.index(n, start)
        return True
    except ValueError:
        return False

只是另一种选择,我个人认为这里的嵌套并没有问题

这是缩短时间:

def spy_game(nums):
    if 0 in nums:
        nums_1 = nums[nums.index(0)+1:]
        if 0 in nums_1:
            if 7 in nums[nums_1.index(0)+1:]:
                return True
    return False
输出:

>>> spy_game([1,0,7])
False
>>> spy_game([0,1,7])
False
>>> spy_game([1,0,1])
False
>>> spy_game([0,0,7])
True

只是另一种选择,我个人认为这里的嵌套并没有问题

这是缩短时间:

def spy_game(nums):
    if 0 in nums:
        nums_1 = nums[nums.index(0)+1:]
        if 0 in nums_1:
            if 7 in nums[nums_1.index(0)+1:]:
                return True
    return False
输出:

>>> spy_game([1,0,7])
False
>>> spy_game([0,1,7])
False
>>> spy_game([1,0,1])
False
>>> spy_game([0,0,7])
True

您的代码隐藏了您试图执行的操作。试试这个选择。尽管它仍然有3个嵌套级别,但它所做的事情更清楚:

def spy_game(nums):
    search_for = [x for x in "007"]
    for n in nums:
        if not search_for:  # found everything we were looking for
            return True
        if n == search_for[0]:  # found the next item
            search_for.pop(0)
return False

您的代码隐藏了您试图执行的操作。试试这个选择。尽管它仍然有3个嵌套级别,但它所做的事情更清楚:

def spy_game(nums):
    search_for = [x for x in "007"]
    for n in nums:
        if not search_for:  # found everything we were looking for
            return True
        if n == search_for[0]:  # found the next item
            search_for.pop(0)
return False

最好设计函数,使其不依赖于特定的输入。例如:

def contains_anywhere(s, search):
    i = 0
    for c in search:
        try:
            i = s.index(c, i) + 1
        except ValueError:
            return False
    return True

ok = contains_anywhere([0,1,1,0,2,2,7,2], [0,0,7])

最好设计函数,使其不依赖于特定的输入。例如:

def contains_anywhere(s, search):
    i = 0
    for c in search:
        try:
            i = s.index(c, i) + 1
        except ValueError:
            return False
    return True

ok = contains_anywhere([0,1,1,0,2,2,7,2], [0,0,7])

不需要
else:return False
,只需底部的
return False
即可完成相同的任务。最后3个else是不必要的,您只需将它们替换为一个return False即可。由于只有一种方法返回True,其他任何方法都会返回False:)不需要
else:return False
,只需底部的
return False
即可完成相同的任务。最后三种方法都是不必要的,您可以用一个返回False替换它们。因为只有一种方法可以返回True,其他任何方法都会返回False:)如果要求是一个数字列表,这就不能正确地将数字与字符串进行比较(使用
search\u for=[int(x)for x in“007”]
),在
[0,0,7]
等情况下,循环在返回
True
之前会中断,移动
if not search\u for:
pop
下的复选框可以解决这个问题,但是我们又回到了3层嵌套。我同意,这会使函数的意图更清晰。如果要求是一个数字列表,则无法正确地将数字与字符串进行比较(使用
search\u for=[int(x)for x in“007”]
),在
[0,0,7]
等情况下,循环会在返回
True
之前中断,移动
if not search\u for:
pop
下的复选框可以解决这个问题,但是我们又回到了3层嵌套。我同意,这使该职能的意图更加明确。