且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在不使用set的情况下从嵌套列表中删除重复项

更新时间:2023-09-10 15:23:58

您可以遍历内部列表并检查该字符是否已存在

You can iterate over the inner list and check if that character is already present or not

inputList = [['c', 'p', 'p'], ['a', 'a', 'a'], ['t', 't', 'p']]
result = []

for l in inputList:
    # create a empty list to store intermediate result 
    tmp = []
    # iterate over sublist
    for ch in l:
        if ch not in tmp: tmp.append(ch)
    result.append(tmp)
print(result)

推荐文章