python 使用set对列表去重,并保持列表原来顺序
# python 使用set对列表去重,并保持列表原来顺序
list1 = ['cc', 'bbbb', 'afa', 'sss', 'bbbb', 'cc', 'shafa']
for item in list1:
print('word: %6s index: %2s' % (item, list1.index(item))) print('==========================')
list2 = list(set(list1))
list2.sort(key=list1.index)
print(list1)
print(list2)
运行结果:

def non_repeat_sort(list1):
list2 = sorted(list(set(list1)), key=lambda e: list1.index(e), reverse=False)
return list2 def join_list_non_repeat_sort(list1, list2):
list3 = list1 + list2
list4 = non_repeat_sort(list3)
return list4 if __name__ == '__main__':
list1 = ['a', 'b', 'c', 'a', 'c', 'c']
list2 = ['c', 'a', 'e', 'f', 'e', 'e'] print('list1: %s ' % list1)
print('list2: %s ' % list2) list11 = non_repeat_sort(list1)
list22 = non_repeat_sort(list2)
print('list11: %s ' % list11)
print('list22: %s ' % list22) result_list = join_list_non_repeat_sort(list1, list2)
# result_list = join_list_non_repeat_sort(list11, list22)
print('result_list: %s ' % result_list)
输出结果:

python 使用set对列表去重,并保持列表原来顺序的更多相关文章
- Python列表去重的三种方法
1. 列表去重 li = [] for item in my_list: if item not in li: li.append(item) 2.集合去重 list(set(my_list)) 3. ...
- Python 基础之列表去重的几种玩法
列表去重 1.方法1 借助一个临时列表 ids = [1,2,3,3,4,2,3,4,5,6,1] news_ids = [] for id in ids: if id not in news_ids ...
- Python 字符串与列表去重
最近面试中出现频率比较高的字符串和列表的去重pstr = 'abcadcf'# 字符串去重# 1.使用集合 --没有保持原来的顺序 print(set(pstr)) # 2.使用字典 -- 没有保持原 ...
- (四)Python中的“四大才子”(字符串、列表、字典、集合)
前戏:在python中把数据序列分为可变(mutable)和不可变(immutable)两种 不可变:string.int.float.tuple 特点:相同对象只是占用一个内存地址,不管有多少个变量 ...
- pyhon列表去重方法总结
开发中对数组.列表去重是非常常见的需求,对一个list中的id进行去重,有下面几种方法,前面两种方法不能保证顺序, 后面两种方法可以保持原来的顺序. 下面的代码都在Python3下测试通过, Pyth ...
- python 全栈开发,Day7(元组转换,列表以及字典的坑,集合,关系测试,深浅copy,编码补充)
一.元组转换 数字 tu = (1) tu1 = (1,) print(tu,type(tu)) print(tu1,type(tu1)) 执行输出: 1 <class 'int'>(1, ...
- day 06 列表去重, 数据类型的补充,编码,深浅copy
因为重要,所以放前面 列表去重 l1 = [1, 2, 3, 4, 5] l2 = [3, 4, 5, 6, 7] set = list(set(l1 + l2)) # set自动去重,然后变成lis ...
- Python小白的发展之路之Python基础(二)【字符串、列表、集合、文件操作】
列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1.列表.元组操作 (1)列表 列表是可变的(mutable)——可以改变列表的内容,这不同于字符串和元组,字符串和元组都是不 ...
- python之最强王者(6)——列表(List)
1.Python 列表(List) 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置 ...
随机推荐
- [Python] Python 之 function, unbound method 和 bound method
首先看一下以下示例.(Python 2.7) #!/usr/bin/env python # -*- coding: utf-8 -*- class C(object): def foo(self): ...
- 初步总结javascript中学习DOM之前的知识
嘿嘿,又到了周末时间,周六其实就是总结这周的学习的,记得周二周三刚开始接触javascript时间,还是不知道怎么学习的,就感觉找不到方向,那时间学习的只是总结了一些简单的定义或者是学习结构,今天就把 ...
- Python错误和异常 学习笔记
错误和异常概念 错误: 1.语法错误:代码不符合解释器或者编译器语法 2.逻辑错误:不完整或者不合法输入或者计算出现问题 异常:执行过程中出现万体导致程序无法执行 1.程序遇到 ...
- cnBlogs博客推荐
数据结构和算法若可以称为为编程的细胞结构,那设计模式就是编程的灵魂气脉. 一个从是编程的微观演绎,一个是编程的宏观设计.这个从技术和艺术的结合体,毫无疑问是在世界末日之前的很伟大的一项发明. 设计 ...
- 使用import取代require
首先,Module 语法是 JavaScript 模块的标准写法,坚持使用这种写法.使用import取代require. // bad const moduleA = require('moduleA ...
- C# EF中调用 存储过程并调回参数
TourEntities db = new TourEntities(); List<v_product> v = new List<v_product>(); SqlPara ...
- 2527: [Poi2011]Meteors[整体二分]
2527: [Poi2011]Meteors Time Limit: 60 Sec Memory Limit: 128 MB Submit: 1528 Solved: 556 [Submit][S ...
- 1007: [HNOI2008]水平可见直线[维护下凸壳]
1007: [HNOI2008]水平可见直线 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 7184 Solved: 2741[Submit][Sta ...
- INTRO: THE DAWN (亡灵序曲) 中独白
As the last ship sailed towards the distant horizon I sat there watching on a rock My mind slowly dr ...
- 使用SQL手动创建数据库并创建一个具有该数据库所有权限的用户
$ mysql -u adminusername -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. ...