小编Ela*_*erg的帖子

获取列表中的所有非唯一元素

我正在尝试构建一个代码,该代码返回一个包含非唯一值的列表,例如[1,2,2,3]=>,[2,2]并且该函数不区分大小写,例如:['p','P','a','b',1,5,6]=> ['p','P'].

这是我到目前为止所提出的:

def non_unique(*data):
    tempLst = [x for x in data if (data.count(x) > 1 if (ord('a') <= ord(x) <= ord('z') or ord('A') <= ord(x) <= ord('Z')) and (data.count(x.upper()) + data.count(x.lower()) > 1)]
    return tempLst
Run Code Online (Sandbox Code Playgroud)

这些是测试示例:

if __name__ == "__main__":
    assert isinstance(non_unique([1]), list)
    assert non_unique([1, 2, 3, 1, 3]) == [1, 3, 1, 3]
    assert non_unique([1, 2, 3, 4, 5]) == []
    assert non_unique([5, 5, 5, 5, 5]) == [5, 5, 5, …
Run Code Online (Sandbox Code Playgroud)

python list python-3.x

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

标签 统计

list ×1

python ×1

python-3.x ×1