栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 函数 列表去重函数实现

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 函数 列表去重函数实现

列表去重函数
定义一个函数 def remove_element(a_list):
将列表[10, 1, 2, 20, 10, 3, 2, 1, 15, 20, 44, 56, 3, 2, 1]
去除重复元素(不能用集合去重,要使用for循环)。

def remove_repeatitive_elements(a_list):
...
函数调用:
my_list = [10, 1, 2, 20, 10, 3, 2, 1, 15, 20, 44, 56, 3, 2, 1]
remove_repeatitive_elements(my_list)

注意点:1、函数内部应该放不会改变的内容,函数外要放会改变的内容

2、使用列表append追加的方法

#准备一个空列表 ,两个列表进行比较 如果列表对象在new_list里面就删除,不在就放进去

my_list = [10, 1, 2, 20, 10, 3, 2, 1, 15, 20, 44, 56, 3, 2, 1]
new_list = []    #定义一个新列表
for item in my_list:           #item为my_list中的元素
    if item not in new_list:   #如果item不在new_list中,进行追加
        new_list.append(item)   #append 列表追加
print(new_list)

###定义函数

def remove_elements(a_list):
    pass

    new_list = []
    for item in a_list:
        if item not in new_list:
            new_list.append(item)   
    print(new_list)

# my_list = [10, 1, 2, 20, 10, 3, 2, 1, 15, 20, 44, 56, 3, 2, 1]
# remove_elements(my_list)#调用函数
m2323_list = [11,4,5,22,2,22,44,4,11,55,5]
remove_elements(m2323_list)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/714103.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号