拉风的咖菲猫
我建议像下面的代码片段一样遍历名称,并将所有现有的首字母缩写保存在一个集合中:all_names = [ 'Risk Management', 'Scope Management', 'Stakeholder Management',]seen = set()def find_initials(name, seen): first, last = name.split() for i in range(1, len(last)+1): for j in range(1, len(first) + 1): initials = first[:j] + last[:i] if initials not in seen: seen.add(initials) return initials # full name is found in seen! for i in range(100): initials = f'{first}{last}{i}' if initials not in seen: seen.add(initials) return initialsinitials = [find_initials(name, seen) for name in all_names]print(initials) # ['RM', 'SM', 'StM']