创建一个函数,接收已读取的两个字符串,返回两个字符串集合的并集,交集,
时间: 2023-05-26 20:06:59 浏览: 102
编写二个函数,其中一个函数func1完成功能为:接收字符串参数,返回一个元组,其中第一个元素为字符串中大写字母个数,
5星 · 资源好评率100%
差集和对称差集。函数名为 set_operations。
```python
def set_operations(str1, str2):
set1 = set(str1.split())
set2 = set(str2.split())
union = set1.union(set2)
intersection = set1.intersection(set2)
difference = set1.difference(set2)
symmetric_difference = set1.symmetric_difference(set2)
return {
'union': union,
'intersection': intersection,
'difference': difference,
'symmetric_difference': symmetric_difference
}
```
函数使用示例:
```python
str1 = 'this is a test string'
str2 = 'this is another test string'
result = set_operations(str1, str2)
print(result['union']) # {'this', 'a', 'is', 'test', 'string', 'another'}
print(result['intersection']) # {'this', 'is', 'test', 'string'}
print(result['difference']) # {'a'}
print(result['symmetric_difference']) # {'another', 'a'}
```
阅读全文