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'}
>>> 
相关推荐
小江的记录本1 天前
【JVM虚拟机】垃圾回收GC:四种引用类型:强引用、软引用、弱引用、虚引用(附《思维导图》+《面试高频考点清单》)
java·jvm·spring boot·后端·python·spring·面试
APIshop1 天前
Python 获取 1688 商品采集 API 接口 | 工厂货源自动化对接商品信息 | 无需选品
运维·python·自动化
deepin_sir1 天前
10 - 函数
开发语言·python
z落落1 天前
C#String字符串
开发语言·c#·php
猫头虎-前端技术1 天前
JS 作用域与闭包:从变量提升到闭包陷阱的超详细解析
开发语言·javascript·云计算·bootstrap·ecmascript·openstack·perl
云泽8081 天前
笔试算法 -位运算篇(二):从唯一字符到消失数字
c++·算法·位运算
charlee441 天前
《GIS基础原理与技术实践》配套案例(Python版)
python·conda·numpy·gis·环境配置
枫叶林FYL1 天前
项目十:事件溯源仓储管理系统(WMS)仿真实现
开发语言·python
ʚ希希ɞ ྀ1 天前
不同路径|| -- dp
算法
繁华落尽,倾城殇?1 天前
[C++11] : atomic,nullptr,default/delete,enum class
开发语言·c++·c++11·nullptr·atomic·enum class·default/delete