小编roo*_*oot的帖子

python 3.6错误或者我疯了

检查列表中的项目是否连续。

[True for x in lst[:-1] for y in lst[1:] if x + 1 == y]
Run Code Online (Sandbox Code Playgroud)

所以列表[1, 2, 3, 4, 5] 返回[True, True, True, True]意味着列表是连续的。

list[1,2,3,4,4] 返回[True, True, True, True]意味着 list 是连续的,但事实并非如此。

为每个语句做证明

lst = [1,2,3,4,4]


def consecutive(lst):    
    lst.sort()
    return [True for x in lst[:-1] for y in lst[1:] if x + 1 == y]


print(consecutive(lst))

some_list = []
for i,j in zip(lst[:-1], lst[1:]):
    if i+1 == j:
        some_list.append(True)
    else:
        some_list.append(False)

print(some_list)
Run Code Online (Sandbox Code Playgroud)

我在这里缺少什么?

python list-comprehension list python-3.x

0
推荐指数
1
解决办法
113
查看次数

标签 统计

list ×1

list-comprehension ×1

python ×1

python-3.x ×1