如何在python中删除相同的字符串

时间:2018-12-12 14:17:17

标签: python string python-3.x

我使用python3.6 我想删除相同的字符串,这是我的代码。

data

结果是:

r = [['I1', 'I2'], ['I1', 'I3'], ['I1', 'I5'], ['I2', 'I3'], ['I2','I4'], ['I2', 'I5']]
for v in range(len(r)):
    for k in range(v+1,len(r)):
        union = list(set(r[v]) | set(r[k]))
        print(sorted(union))

如何删除相同的字符串?

1 个答案:

答案 0 :(得分:3)

我创建了Union_list来跟踪已经打印的内容:

Union_list = [] 
r = [['I1', 'I2'], ['I1', 'I3'], ['I1', 'I5'], ['I2', 'I3'], ['I2','I4'], ['I2', 'I5']]
for v in range(len(r)):
    for k in range(v+1,len(r)):
        union = sorted(list(set(r[v]) | set(r[k])))
        if union not in Union_list:
            Union_list.append(union)
            print(union)

输出:

['I1', 'I2', 'I3']
['I1', 'I2', 'I5']
['I1', 'I2', 'I4']
['I1', 'I3', 'I5']
['I1', 'I2', 'I3', 'I4']
['I1', 'I2', 'I3', 'I5']
['I1', 'I2', 'I4', 'I5']
['I2', 'I3', 'I4']
['I2', 'I3', 'I5']
['I2', 'I4', 'I5']