Python实现嵌套列表去重方法示例

作者:_Apache 时间:2021-01-29 03:56:57 

发现问题

python嵌套列表大家应该都不陌生,但最近遇到了一个问题,这是工作中遇到的一个坑,首先看一下问题


raw_list = [["百度", "CPY"], ["京东", "CPY"], ["黄轩", "PN"], ["百度", "CPY"]]

列表嵌套了列表,并且有一个重复列表["百度", "CPY"],现在要求将这个重复元素进行去重(重复是指嵌套的列表内两个元素都相同),并且保证元素顺序不变,输出还是嵌套列表,即最后结果应该长这样:[["百度", "CPY"], ["京东", "CPY"], ["黄轩", "PN"]]

正常Python去重都是使用set,所以我这边也是用这种思想处理一下


In [8]: new_list = [list(t) for t in set(tuple(_) for _ in raw_list)]
In [9]: new_list
Out[9]: [['京东', 'CPY'], ['百度', 'CPY'], ['黄轩', 'PN']]

=。=以为大功告成,结果发现嵌套列表顺序变了

好吧一步步找一下是从哪边顺序变了的


In [10]: s = set(tuple(_) for _ in raw_list)
In [11]: s
Out[11]: {('京东', 'CPY'), ('百度', 'CPY'), ('黄轩', 'PN')}

恍然大悟关于set的两个关键词:无序 和 不重复 =。=

所以从set解决排序问题基本无望了,然而我还没有放弃,现在问题就变成了对于new_list怎么按照raw_list元素顺序排序,当然肯定要通过sort实现

翻一下Python文档找到以下一段话

文档地址


sort(*, key=None, reverse=False)

This method sorts the list in place, using only < comparisons between
items. Exceptions are not suppressed - if any comparison operations  
fail, the entire sort operation will fail (and the list will likely be left in a
partially modified state).

[`sort()`](https://docs.python.org/3/library/stdtypes.html?highlight=sort#list.sort "list.sort")

accepts two arguments that can only be passed by keyword ( [keyword-only arguments](https://docs.python.org/3/glossary.html#keyword-only-parameter) ):

key specifies a function of one argument that is used to extract a
comparison key from each list element (for example, key=str.lower).
The key corresponding to each item in the list is calculated once and  then used for the entire sorting process. The default value of None
means that list items are sorted directly without calculating a separate
key value.

开始划重点:

sort方法通过参数key指定一个方法,换句话说,key参数的值是函数。

这个函数和new_list上的每个元素会产生一个结果,sort通过这个结果进行排序。

于是这里就想到求出new_list里的每一个元素在raw_list里的索引,根据这个索引进行排序。

代码实现如下:


In [13]: new_list.sort(key=raw_list.index)
In [14]: new_list
Out[14]: [['百度', 'CPY'], ['京东', 'CPY'], ['黄轩', 'PN']]

结果和期望一样 =。=

来源:https://www.jianshu.com/p/3eff6fbd0adc

标签:python,嵌套列表,去重
0
投稿

猜你喜欢

  • python 如何读取列表中字典的value值

    2021-01-27 15:48:31
  • python3 http提交json参数并获取返回值的方法

    2023-09-26 12:30:06
  • Python2.x版本中maketrans()方法的使用介绍

    2021-02-14 21:04:25
  • Python标准库sched模块使用指南

    2022-09-20 12:19:06
  • SQL Server 2005中利用xml拆分字符串序列

    2009-01-06 11:30:00
  • SQL Server数据库连接中常见的错误分析

    2009-01-15 12:51:00
  • Python制作数据导入导出工具

    2023-07-25 06:59:59
  • Python处理excel与txt文件详解

    2021-12-13 20:38:20
  • 低效的键盘和高效的登录框

    2007-08-22 09:17:00
  • python进阶教程之函数对象(函数也是对象)

    2022-08-28 01:06:42
  • python2.7安装图文教程

    2021-01-02 01:36:15
  • Python中的np.vstack()和np.hstack()详解

    2022-05-09 03:06:56
  • Django如何实现上传图片功能

    2023-04-23 17:35:01
  • python中dump与dumps实现序列化

    2023-01-13 22:46:16
  • python数据可视化Pyecharts库sankey修改桑葚图颜色

    2023-09-04 08:56:13
  • django 基于中间件实现限制ip频繁访问过程详解

    2022-06-24 08:38:43
  • python爬虫把url链接编码成gbk2312格式过程解析

    2023-04-26 21:00:10
  • Python用61行代码实现图片像素化的示例代码

    2021-08-27 13:10:55
  • Python3操作MongoDB增册改查等方法详解

    2021-09-20 05:13:49
  • php获取文章内容第一张图片的方法示例

    2023-11-09 18:55:14
  • asp之家 网络编程 m.aspxhome.com