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'}
>>> 
相关推荐
励志的小陈4 小时前
贪吃蛇(C语言实现,API)
c语言·开发语言
kishu_iOS&AI4 小时前
深度学习 —— 损失函数
人工智能·pytorch·python·深度学习·线性回归
Makoto_Kimur4 小时前
java开发面试-AI Coding速成
java·开发语言
好运的阿财4 小时前
OpenClaw工具拆解之canvas+message
人工智能·python·ai编程·openclaw·openclaw工具
laowangpython4 小时前
Gurobi求解器Matlab安装配置教程
开发语言·其他·matlab
wengqidaifeng4 小时前
python启航:1.基础语法知识
开发语言·python
观北海4 小时前
Windows 平台 Python 极简 ORB-SLAM3 Demo,从零实现实时视觉定位
开发语言·python·动态规划
FreakStudio5 小时前
做了个Claude Code CLI 电子宠物:程序员的实体监工代码搭子
python·单片机·嵌入式·面向对象·并行计算·电子diy·电子计算机
知识浅谈5 小时前
DeepSeek V4 和 GPT-5.5 在同一天发布了??我也很懵,但对比完我悟了
算法
柴米油盐那点事儿5 小时前
python+mysql+bootstrap条件搜索分页
python·mysql·flask·bootstrap