小编nom*_*432的帖子

检查字符串是否定义了颜色

我正在寻找一种方法来检查字符串是否定义了颜色,因为我的程序依赖于用户输入颜色,并且当他们输入错误的颜色时它会中断。我怎样才能做到这一点?这里有些例子:

check_color("blue") > True
check_color("deep sky blue") > True
check_color("test") > False
check_color("#708090") > True
Run Code Online (Sandbox Code Playgroud)

python colors

7
推荐指数
1
解决办法
3742
查看次数

程序的断言测试,用于检查列表是否是另一个列表中的子列表

我写了一个小程序,应检查给定列表是否是来自另一个列表的子列表,并返回TrueFalse:

def is_sublist_of(sublist, given):
    """ Returns whether the sublist is part of the given combination.
    The order of the sublist must also correspond to the order of the
    corresponding part in the given combination."""

    return sublist in [given[i:i+len(sublist)] for i in range(0,len(given)-len(sublist))]
Run Code Online (Sandbox Code Playgroud)

此代码是我必须执行的赋值的一部分,但给定的断言之一是:

simple_list = [1, 2, 3, 4]
for element in simple_list:
    assert is_sublist_of([element], simple_list)
assert not is_sublist_of([5], simple_list)
Run Code Online (Sandbox Code Playgroud)

我的程序未通过此测试.这是否意味着我的程序在某些特殊情况下不起作用?感谢您对此进行调查.

python assert list sublist

3
推荐指数
1
解决办法
59
查看次数

标签 统计

python ×2

assert ×1

colors ×1

list ×1

sublist ×1