Python输出两个字符串中相同的字符

2022-03-11 00:00:00 字符串 输出 字符

Python输出两个字符串中相同的字符,计算两个字符串的交集,代码使用了集合,最后的输出结果的字符顺序可能会与原字符串不同。

"""
作者:皮蛋编程(https://www.pidancode.com)
创建日期:2022/3/22
功能描述:Python输出两个字符串中相同的字符
"""

magic_chars = set('abracadabra')
poppins_chars = set('supercalifragilisticexpialidocious')
print(''.join(magic_chars & poppins_chars))  # 集合的交集

输出:
crda

以上代码在python3.9环境下测试通过

相关文章