加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 12:00:17 所属栏目:Python 来源:网络整理
导读:我正在尝试编写一个函数来测试列表是否具有连续数字但是具有非常奇怪的捕获.问题是“a”可以用作任何整数的替代,但列表中至少有2个元素必须是数字.元素 = 1(如果不是“a”)并且是整数.可以假设输入是这种形式,因此不需要检查它. 我是编码的新手,所以我实际上
我正在尝试编写一个函数来测试列表是否具有连续数字但是具有非常奇怪的捕获.问题是“a”可以用作任何整数的替代,但列表中至少有2个元素必须是数字.元素> = 1(如果不是“a”)并且是整数.可以假设输入是这种形式,因此不需要检查它.
我是编码的新手,所以我实际上更喜欢在一个衬垫上进行循环,因为我还不熟悉一个衬垫的使用.

例如:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读