Python - Intersectiing strings

前端 未结 6 595
北海茫月
北海茫月 2021-01-18 08:26

Trying to write a for function that takes two strings and returns the characters that intersect in the order that they appear in the first string.

Here\'s what I tri

6条回答
  •  失恋的感觉
    2021-01-18 08:28

    def str_intersection(str1, str2):
        common_letters = set(str1) & set(str2)
        str3 = ''
        for c in str1:
            if (c in common_letters) and (c not in str3):
                str3 += c
        return str3
    

提交回复
热议问题