Python语言基础与应用-北京大学-陈斌-P32-31-计算和控制流-上机练习:创建并调用函数-字符集合的并集-上机代码

Python语言基础与应用-北京大学-陈斌-P32-31-计算和控制流-上机练习:创建并调用函数-字符集合的并集-上机代码

本文环境: win10 + Thonny4.1.4

python 复制代码
# 函数训练字符集合的并集
def my_union(str1,str2):
    list1 = []
    list2 = []
        
    i = 0    
    while i < len(str1):
        list1.append(str1[i])
        i += 1
 
    i = 0
    while i < len(str2):
        list2.append(str2[i])
        i += 1

    i = 0
    while i < len(list2):
        if list2[i] not in list1:
            list1.append(list2[i])
        i += 1    
    
    return(set(list1))

str1 = input("请输入字符串1:")
str2 = input("请输入字符串2:")

print(my_union(str1,str2))

结果:

python 复制代码
>>> %Run 111111.py
请输入字符串1:abcdefg
请输入字符串2:efghijk
{'e', 'h', 'c', 'j', 'k', 'b', 'f', 'i', 'd', 'a', 'g'}
>>> 
相关推荐
中草药z12 分钟前
【嵌入模型】概念、应用与两大 AI 开源社区(Hugging Face / 魔塔)
人工智能·算法·机器学习·数据集·向量·嵌入模型
de之梦-御风12 分钟前
【C#.Net】C#开发的未来前景
开发语言·c#·.net
web3.088899915 分钟前
微店商品详情API实用
python·json·时序数据库
知乎的哥廷根数学学派34 分钟前
基于数据驱动的自适应正交小波基优化算法(Python)
开发语言·网络·人工智能·pytorch·python·深度学习·算法
de之梦-御风1 小时前
【C#.Net】C#在工业领域的具体应用场景
开发语言·c#·.net
sunfove1 小时前
将 Python 仿真工具部署并嵌入个人博客
开发语言·数据库·python
Learner1 小时前
Python类
开发语言·python
ADI_OP1 小时前
ADAU1452的开发教程10:逻辑算法模块
算法·adi dsp中文资料·adi dsp·adi音频dsp·adi dsp开发教程·sigmadsp的开发详解
xingzhemengyou11 小时前
C语言 查找一个字符在字符串中第i次出现的位置
c语言·算法
2501_941329721 小时前
门及其组件定位识别_YOLO13-C3k2-PoolingFormer改进模型研究
python