从列表中使用相同字符内容从python中删除元素?

时间:2018-11-28 16:20:58

标签: python-3.x list for-loop append

我有以下列表

['KAK1K2','KAK1K3','KAK1K4','KAK1K5','KAK1K6','KAK2K1','KAK2K3','KAK2K4','KAK2K5','KAK2K6','KAK3K1','KAK3K2 ','KAK3K4','KAK3K5','KAK3K6','KAK4K1','KAK4K2','KAK4K3','KAK4K5','KAK4K6','KAK5K1','KAK5K2','KAK5K3','KAK5K4 'KAK5K6','KAK6K1','KAK6K2','KAK6K3','KAK6K4','KAK6K5','K1KAK2','K1KAK3','K1KAK4','K1KAK5','K1KAK6','K1K2K2' ','K1K2K4','K1K2K5','K1K2K6','K1K3KA','K1K3K2','K1K3K4','K1K3K5','K1K3K6','K1K4KA','K1K4K2','K1K4K3 'K1K4K6','K1K5KA','K1K5K2','K1K5K3','K1K5K4','K1K5K6','K1K6KA','K1K6K2','K1K6K3','K1K6K4','K1K2K2' ','K2KAK4','K2KAK5','K2KAK6','K2K1KA','K2K1K3','K2K1K4','K2K1K5','K2K1K6','K2K3KA','K2K3K1','K2K3K4' 'K2K3K6','K2K4KA','K2K4K1','K2K4K3','K2K4K5','K2K4K6','K2K5KA','K2K5K1','K2K5K3','K2K5K4','K2K5K6' ,'K2K6K3','K2K6K4','K2K6K5','K3KAK1','K3KAK2','K3KAK4','K3KAK5','K3KAK6','K3K1KA','K3K1K2','K3K1K4',' 'K3K1K 6','K3K2KA','K3K2K1','K3K2K4','K3K2K5','K3K2K6','K3K4KA','K3K4K1','K3K4K2','K3K4K5','K3K4K6','K3K5KA ,'K3K5K2','K3K5K4','K3K5K6','K3K6KA','K3K6K1','K3K6K2','K3K6K4','K3K6K5','K4KAK1','K4KAK2','K4KAK3' K4KAK6','K4K1KA','K4K1K2','K4K1K3','K4K1K5','K4K1K6','K4K2KA','K4K2K1','K4K2K3','K4K2K5','K4K2K6','K4K2K6' ,'K4K3K2','K4K3K5','K4K3K6','K4K5KA','K4K5K1','K4K5K2','K4K5K3','K4K5K6','K4K6KA','K4K6K1','K4K6K2 K4K6K5','K5KAK1','K5KAK2','K5KAK3','K5KAK4','K5KAK6','K5K1KA','K5K1K2','K5K1K3','K5K1K4','K5K1K6','K5K2 ,'K5K2K3','K5K2K4','K5K2K6','K5K3KA','K5K3K1','K5K3K2','K5K3K4','K5K3K6','K5K4KA','K5K4K1','K5K4K4 K5K4K6','K5K6KA','K5K6K1','K5K6K2','K5K6K3','K5K6K4','K6KAK1','K6KAK2','K6KAK3','K6KAK4','K6KAK5','K6K1 ,'K6K1K3','K6K1K4','K6K1K5','K6K2KA','K6K2K1','K6K2K3','K6K2K4','K6K2K5','K6K3KA','K6K3K1','K6K3K2 K6K3K 5”,“ K6K4KA”,“ K6K4K1”,“ K6K4K2”,“ K6K4K3”,“ K6K4K5”,“ K6K5KA”,“ K6K5K1”,“ K6K5K2”,“ K6K5K3”,“ K6K5K4”]

使用以下代码生成的

atoms = ['KA', 'K1', 'K2','K3', 'K4', 'K5', 'K6']
combos = []

for e1 in atoms:
    for e2 in atoms:
        for e3 in atoms:
            if e1 != e2 != e3 != e1:
                combos.append(e1+e2+e3)

我愿意删除多余的元素。 例如,保留“ KAK1K2”并删除KAK2K1,“ K1KAK2”,“ K1K2KA等”。

非常感谢

1 个答案:

答案 0 :(得分:0)

一种解决方案是将所有known组合存储为frozenset,并检查是否已经获得该组合。仅在没有的情况下添加:

atoms = ['KA', 'K1', 'K2','K3', 'K4', 'K5', 'K6']
combos = []
known = set()

for e1 in atoms:
    for e2 in atoms:
        for e3 in atoms:
            if e1 != e2 != e3 != e1:
                fz = frozenset( {e1,e2,e3})
                if fz not in known:
                    known.add(fz)
                    combos.append(e1+e2+e3)
print(combos)

['KAK1K2', 'KAK1K3', 'KAK1K4', 'KAK1K5', 'KAK1K6', 'KAK2K3', 'KAK2K4', 'KAK2K5', 
'KAK2K6', 'KAK3K4', 'KAK3K5', 'KAK3K6', 'KAK4K5', 'KAK4K6', 'KAK5K6', 'K1K2K3',
'K1K2K4', 'K1K2K5', 'K1K2K6', 'K1K3K4', 'K1K3K5', 'K1K3K6', 'K1K4K5', 'K1K4K6', 
'K1K5K6', 'K2K3K4', 'K2K3K5', 'K2K3K6', 'K2K4K5', 'K2K4K6', 'K2K5K6', 'K3K4K5', 
'K3K4K6', 'K3K5K6', 'K4K5K6']

itertools combinations使用Patrick Haugh的较短建议方法是:

from itertools import combinations

combos = [''.join(x) for x in combinations(atoms,3)]
print(combos) 

['KAK1K2', 'KAK1K3', 'KAK1K4', 'KAK1K5', 'KAK1K6', 'KAK2K3', 'KAK2K4', 'KAK2K5',
'KAK2K6', 'KAK3K4', 'KAK3K5', 'KAK3K6', 'KAK4K5', 'KAK4K6', 'KAK5K6', 'K1K2K3',
'K1K2K4', 'K1K2K5', 'K1K2K6', 'K1K3K4', 'K1K3K5', 'K1K3K6', 'K1K4K5', 'K1K4K6', 
'K1K5K6', 'K2K3K4', 'K2K3K5', 'K2K3K6', 'K2K4K5', 'K2K4K6', 'K2K5K6', 'K3K4K5', 
'K3K4K6', 'K3K5K6', 'K4K5K6']