. - 力扣(LeetCode) 正面直接解就行了,先找出重复的项,再通过重复的项找出所有下标进行合并操作最后去重排序 from typing import List class Solution: def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]: new_array = [] for item in accounts: new_array.extend(item[1:]) duplicates = {item for item in new_array if new_array.count(item) > 1} for email in duplicates: duplicates_index = [] for index, item in enumerate(accounts): if email in item[1:]: duplicates_index.append(index) for item in sorted(duplicates_index[1:], reverse=True): emails = accounts[duplicates_index[0]][1:] + accounts[item][1:] accounts[duplicates_index[0]] = [accounts[duplicates_index[0]][0]] + emails accounts.pop(item) for item in accounts: item[1:] = sorted(list(set(item[1:]))) return accounts accounts = [["David", "David0@m.co", "David4@m.co", "David3@m.co"], ["David", "David5@m.co", "David5@m.co", "David0@m.co"], ["David", "David1@m.co", "David4@m.co", "David0@m.co"], ["David", "David0@m.co", "David1@m.co", "David3@m.co"], ["David", "David4@m.co", "David1@m.co", "David3@m.co"]] print(Solution().accountsMerge(accounts))