python - 检查具有特定条件的连续数字 - Python

标签 python

我正在尝试编写一个函数来测试列表是否有连续的数字,但有一个非常奇怪的问题。问题是“a”可以用作任何整数的替代品,但列表中至少有 2 个元素必须是数字。还有元素 >= 1(如果不是“a”)并且是整数。可以假设输入是这种形式,所以不需要检查它。 我是编码新手,所以我实际上更喜欢 for 循环而不是一个衬里,因为我还不太熟悉一个衬里的使用。

例如:

>>>check_consecutive([1,2,"a","a",5,6,7,"a"])
True
>>>check_consecutive(["a","a","a","a",9,10])
True
>>>check_consecutive(["a","a","a","a",5])
False #Need at least 2 elements to be numbers
>>>check_consecutive([3,4,5,"a",6,7])
False
>>>check_consecutive(["a","a","a","a","a","a",2,3])
False

我尝试做的事情:

def check_consecutive(lst):
 count = 0
 for i in range(len(lst)-1):
     if lst[i] == "a" or lst[i+1] == "a":
         count +=1
     elif lst[i+1] - lst[i] == 1:
         count +=1
 if count == len(lst)-1:
     return True
 return False

这不起作用,因为它没有考虑到“a”的值。我不知道该怎么做。在此先感谢您的帮助。

最佳答案

尝试以下操作。它匹配您所有的测试用例,并且我将单行代码保持在最低限度:

def check_consecutive(lst):
    # Check for the number of non-"a" entries in the list:
    if len([x for x in lst if x != "a"]) < 2:
        return False

    # Get the first non-a value (ASSUMPTION: this is a number)
    first_number = next((e for e in lst if e != "a"), None)

    # Find the index of the first number
    first_index = lst.index(first_number)

    # Find the difference between the first number and its index
    diff = first_number - first_index

    # Based on the final example - false if negative values would be used:
    if diff < 0:
        return False

    # Create a new list - replace a's with their index plus the difference we found
    # If the list is consecutive, this difference will be consistent for all values
    all_numbers = []
    for i, x in enumerate(lst):
        if x == "a":
            all_numbers.append(i + diff)
        else:
            all_numbers.append(x)

    # Check if the values are now consecutive or not!
    if all(all_numbers[i+1] == all_numbers[i] + 1 for i in range(len(all_numbers) - 1)):
        return True
    else:
        return False

print check_consecutive([1,2,"a","a",5,6,7,"a"])
#True
print check_consecutive(["a","a","a","a",9,10])
#True
print check_consecutive(["a","a","a","a",5])
#False #Need at least 2 elements to be numbers
print check_consecutive([3,4,5,"a",6,7])
#False
print check_consecutive(["a","a","a","a","a","a",2,3])
#False

如果你想看看一些单行代码是如何工作的,你可以按如下方式稍微减少函数:

def check_consecutive(lst):
    # Check for the number of non-"a" entries in the list:
    if len([x for x in lst if x != "a"]) < 2:
        return False

    # Get the first non-a value (ASSUMPTION: this is a number)
    first_number = next((e for e in lst if e != "a"), None)

    # Find the index of the first number
    first_index = lst.index(first_number)

    # Find the difference between the first number and its index
    diff = first_number - first_index

    # Based on the final example - false if negative values would be used:
    if diff < 0:
        return False

    if all(x == "a" or x == i + diff for i, x in enumerate(lst)):
        return True
    else:
        return False

关于python - 检查具有特定条件的连续数字 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44022382/

相关文章:

python - win32com 不发送附件

python - 在 Arelle 开始时出现错误逃生错误

python - PySide:将 QComboBox 索引与字符串连接

python - autopy 可以在本地安装吗?

jquery - 已将 JSON 数据发送至 DJANGO。现在我无法取回它

python - 使用某些投影时 Basemap.contour() 出现 IndexError

python - 具有仿射间隙惩罚的 Smith-Waterman 算法中的回溯

python - 在 python 中折叠 DNA 序列的正向和反向补码的算法?

python - 二维 numpy 数组的重采样

python - 为什么在函数conway的Python游戏中出现这种语法错误?