需求:

input: ['cat', 'dog', 'tac', 'god', 'act', 'dgo']
output: [['cat', 'tac', 'act'], ['dog', 'god', 'dgo']]

代码实现:

lst = ['cat', 'dog', 'tac', 'god', 'act', 'dgo']
dic = {}
for i in lst:
    j = sorted(list(i))
    j = tuple(j)
    if j not in dic:
        dic[j] = [i]
    else:
        dic[j].append(i)
print(list(dic.values()))

类似文章