一个列表有多个字符串,所以如果另一个字符串中不存在一个字符串,我想连接两个字符串

时间:2021-06-15 04:09:46

标签: python string

def conc():
    l1=l
    print(l1[-1])
    for i in range(len(l1)-1):
        if l1[i] not in l1[i+1]:
            s=l1[i]+l1[i+1]
            print(s)
        if l1[i] not in l1[-1]:
            s1=l1[i]+l1[-1]
            print(s1)

l=ast.literal_eval(input())
conc()

给定

l=["abc","yyy","def","csv"]

结果应该是

abcdef,defabc,defcsv,csvdef

yyy 没有添加,因为它有重复的字符串

如何实现?

1 个答案:

答案 0 :(得分:0)

使用嵌套循环遍历所有单词对(您也可以使用 itertools.product())。然后检查单词之间是否有任何共同的字符。

for word1 in l:
    for word2 in l:
        if not any(c in word1 for c in word 2):
            print(word1+word2)