📜  Python|检查 Non-None 值是否连续

📅  最后修改于: 2022-05-13 01:55:40.932000             🧑  作者: Mango

Python|检查 Non-None 值是否连续

有时,在使用Python列表时,我们可能会遇到需要查找所有值是否有效(非无)的问题。这在日常编程中有很多应用。让我们讨论一种可以执行此任务的方法。

方法:使用迭代器 + all() + any()
上述功能的组合可用于执行此特定任务。在此,我们使用初始 all() 过滤前导 None 值,然后使用 any() 检查奇异有效值子列表,然后检查所有必需的 None 值。如果以上任何一个返回false。 Non-None 值不连续。

# Python3 code to demonstrate working of
# Check if Non-None values are contiguous
# Using iterator + all() + any()
  
# helper function 
def check_cont(test_list):
    test_list = iter(test_list)
    all(x is None for x in test_list)        
    any(x is None for x in test_list)      
    return all(x is None for x in test_list)
  
# initializing list 
test_list = [None, None, 'Gfg', 'is', 'Good', None, None, None]
  
# printing list 
print("The original list : " + str(test_list))
  
# Check if Non-None values are contiguous
# Using iterator + all() + any()
res = check_cont(test_list)
  
# Printing result
print("Are non-none values contiguous ? : " + str(res))
输出 :
The original list : [None, None, 'Gfg', 'is', 'Good', None, None, None]
Are non-none values contiguous ? : True