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'}
>>> 
相关推荐
cup112 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi004 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵6 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf7 小时前
Agent 流程编排
后端·python·agent
copyer_xyf7 小时前
Agent RAG
后端·python·agent
copyer_xyf7 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf7 小时前
Agent 记忆管理
后端·python·agent
JieE21215 小时前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python