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'}
>>> 
相关推荐
韭菜炒鸡肝天7 分钟前
VTK开发笔记(一):VTK介绍,Qt..+VSx+VTK.编译
开发语言·笔记·qt
The Chosen One9858 分钟前
高进度算法模板速记(待完善)
java·前端·算法
Gu Gu Study11 分钟前
ScoutLoop开放域深度研究引擎(agent的初步设计想法)
人工智能·python
Aaron - Wistron44 分钟前
Web API C# (Furion版)带 单元测试
开发语言·后端·c#
卷无止境1 小时前
写代码这件事,到底该讲究点什么?
后端·python
卷无止境1 小时前
循环复杂度到底在算什么,Python 代码怎么才能写得让人一看就懂
后端·python
lpfasd1231 小时前
MediaCrawler 项目深度分析
chrome·python·chrome devtools
Dxy12393102162 小时前
Python项目打包成EXE完整教程(PyInstaller实战避坑)
开发语言·python
bamb002 小时前
一个项目带你入门AI应用开发01
python
0566462 小时前
Python康复训练——常用标准库
开发语言·python·学习