-
2022-01-08 13:15:04
列表去重函数 定义一个函数 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)
更多相关内容 -
python列表去重的二种方法
2021-01-20 04:57:07您可能感兴趣的文章:Python3删除排序数组中重复项的方法分析Python实现删除排序数组中重复项的两种方法示例python中对list去重的多种方法Python对列表去重的多种方法(四种方法)Python对字符串实现去重操 -
你应该知道的python列表去重方法
2020-12-24 23:03:22列表去重是写Python脚本时常遇问题,因为不管源数据来自哪里,当我们转换成列表的方式时,有可能预期的结果不是我们最终的结果,最常见的就是列表中元素有重复,这时候第一件事我们就要做去重处理。 我们先来个最... -
对python中两种列表元素去重函数性能的比较方法
2020-09-20 07:49:21今天小编就为大家分享一篇对python中两种列表元素去重函数性能的比较方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 -
python 列表去重
2022-04-08 14:55:58python去重 通过内置的数据结构去重 使用set数据类型直接进行去重 当要去重的元素是dict或者[dict]就不是很理想 通过set去重字典 通过set去重[dict] 可以直接通过set去重列表、元组、字符串 a = (1, 2, 2, 1) ...python去重
通过内置的数据结构去重
- 使用set数据类型直接进行去重
当要去重的元素是dict或者[dict]就不是很理想
通过set去重字典
通过set去重[dict]
可以直接通过set去重列表、元组、字符串
a = (1, 2, 2, 1) print("去重元组", set(a)) # 去重元组 # a = [1, 2, 3, 2] print("去重列表", set(a)) # 去重列表 # a = '123123' print("去重字符串", set(a)) # 去重字符串
- 通过字典的key去重
因为字典的key是不可以重复的,所以我们就可以通过字典的key来进行去重.
a = [1, 2, 3, 1, 2] dict1 = {} for i in a: dict1[i] = '' print("通过字典的可以来进行去重", dict1.keys())
- 通过python内置的函数来去重
我们可以使用reduce这个方法来进行去重
官方的一段介绍
Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
加粗样式a = [1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7] def fun1(a, b): if not str(b) in a: a.append(str(b)) return a c = reduce(fun1, [[], ] + a) print(c)
- 使用set数据类型直接进行去重
-
对python列表里的字典元素去重方法详解
2020-09-19 17:17:04今天小编就为大家分享一篇对python列表里的字典元素去重方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 -
Python对列表去重的多种方法(四种方法)
2020-12-23 20:46:10无聊统计了下列表去重到底有多少种方法。下面小编给大家总结一下,具体内容详情如下; 开发中对数组、列表去重是非常常见的需求,对一个list中的id进行去重,有下面几种方法,前面两种方法不能保证顺序, 后面两种... -
Python 利用内置set函数对字符串和列表进行去重的方法
2020-09-20 07:51:38今天小编就为大家分享一篇Python 利用内置set函数对字符串和列表进行去重的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 -
python 列表去重及sort用法
2022-02-27 08:10:08去重(均保持了原来的顺序) ① orgList = [1,5] formatList = list(set(orgList)) formatList.sort(key=orgList.index) print (formatList) Sort 默认reverse=False 升序 ① x = ['mmm', 'mm', 'mm'...去重(均保持了原来的顺序)
①
orgList = [1,5]
formatList = list(set(orgList))
formatList.sort(key=orgList.index)
print (formatList)
Sort
默认reverse=False 升序
①
x = ['mmm', 'mm', 'mm', 'm' ]
x.sort(key = len)
>>['m', 'mm', 'mm', 'mmm']②
d =['CCC', 'bb', 'ffff', 'z']
>>> sorted(d,key = str.lower ) #将列表中的每个元素变为小写,再按每个元素中的每个字母的ascii码从小到大排序
['bb', 'CCC', 'ffff', 'z']③双重列表/元组
L = [(12, 12), (34, 13), (32, 15), (12, 24), (32, 64), (32, 11)]
若希望按照第一列排序,则
a.sort(key=lambda x: (x[0]))
可设置reverse=True则按照第一列降序排序
若希望首先按照第一列排,第一列相同的情况下,按照第二列排,均是升序的方式,则
a.sort(key=lambda x: (x[0], x[1]))
若第一列升序,第二列降序,则
l=[ [1,2,3],a.sort(key=lambda x: (x[0], -x[1]))
[2,2,1]]
a=sorted(l, key = lambda x:(x[2])) #按照第三个数字大小排序
>>>[[2, 2, 1], [1, 2, 3]] 转载于: python多重排序_Jum_Summer的博客-CSDN博客_python 多重排序④字典:
f=[{'age': 20, 'name': 'abc'}, {'age': 25, 'name': 'ghi'}, {'age': 30, 'name': 'def'}]
f2 = sorted(f,key = lambda x:x['age'])
>>> [{'age': 20, 'name': 'abc'}, {'age': 25, 'name': 'ghi'}, {'age': 30, 'name': 'def'}]⑤operator.itemgetter函数
转载于:Python中的sort()方法使用基础 - 路永远在脚下 - 博客园
from operator import itemgetter
alist = [(2,3,10), (1,2,3), (5,6,7), (2,5,10), (2,4,10)]
# 多级排序,先按照第3个元素排序,然后按照第2个元素排序:
(在第3个元素相同情况下再以第2个元素排序)
print (sorted(alist,key = itemgetter(2,1)))print
sorted
(alist
, key
=
lambda
x:(int(x[2]),int(x[1]))
>>>[(1, 2, 3), (5, 6, 7), (2, 3, 10), (2, 4, 10), (2, 5, 10)]
-
python pandas dataframe 去重函数的具体使用
2020-09-16 12:16:11主要介绍了python pandas dataframe 去重函数的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 -
python去重函数是什么
2021-02-04 03:11:59数据去重可以使用duplicated()和drop_duplicates()两个方法。DataFrame.duplicated(subset = None,keep =‘first’)返回boolean Series表示重复行参数:subset:列标签或标签序列,可选仅考虑用于标识重复项的某些...数据去重可以使用duplicated()和drop_duplicates()两个方法。
DataFrame.duplicated(subset = None,keep =‘first’)返回boolean Series表示重复行
参数:
subset:列标签或标签序列,可选
仅考虑用于标识重复项的某些列,默认情况下使用所有列
keep:{‘first’,‘last’,False},默认’first’
first:标记重复,True除了第一次出现。
last:标记重复,True除了最后一次出现。
错误:将所有重复项标记为True。
相关推荐:《Python基础教程》import numpy as np
import pandas as pd
from pandas import Series, DataFrame
df = pd.read_csv('./demo_duplicate.csv')
print(df)
print(df['Seqno'].unique()) # [0. 1.]
# 使用duplicated 查看重复值
# 参数 keep 可以标记重复值 {'first','last',False}
print(df['Seqno'].duplicated())
'''
0 False
1 True
2 True
3 True
4 False
Name: Seqno, dtype: bool
'''
# 删除 series 重复数据
print(df['Seqno'].drop_duplicates())
'''
0 0.0
4 1.0
Name: Seqno, dtype: float64
'''
# 删除 dataframe 重复数据
print(df.drop_duplicates(['Seqno'])) # 按照 Seqno 来去重
'''
Price Seqno Symbol time
0 1623.0 0.0 APPL 1473411962
4 1649.0 1.0 APPL 1473411963
'''
# drop_dujplicates() 第二个参数 keep 包含的值 有: first、last、False
print(df.drop_duplicates(['Seqno'], keep='last')) # 保存最后一个
'''
Price Seqno Symbol time
3 1623.0 0.0 APPL 1473411963
4 1649.0 1.0 APPL 1473411963
'''
-
python列表常见的5种去重方法
2021-12-26 11:04:10列表去重在python面试和实际运用中,十分常见,也是最基础的重点知识。 以下总结了5种常见的列表去重方法 一、使用for循环实现列表去重 此方法去重后,原顺序保持不变。 # for循环实现列表去重 list1 = ['a', 'b', 1... -
python中列表去重
2019-07-02 18:53:41如果要删除列表列表中的重复项,则同样可以用下面的几种方法来处理 方法一: >> > data = [ 2 , 1 , 3 , 4 , 1 ] >> > [ item for item in data if data . count ( item ) =... -
Python嵌套列表去重
2020-11-26 09:05:45人生苦短早用Python这是工作中遇到的一个坑,首先看一下问题raw_list = [["百度", "CPY"], ["京东", "CPY"], ["黄轩", "PN"], ["百度", "CPY"]]列表嵌套了列表,并且有一个重复列表["百度", "CPY"],现在要求将这个... -
python列表如何去重
2021-02-03 16:15:15python列表去重的方法:1、利用字典的【fromkeys()】和【keys()】方法去重;2、集合的可迭代方法;3、用for循环,代码为【for x in L3:if x not in L4:L4.append(x)】。python列表去重的方法:第一种方法,利用字典... -
Python 列表去重
2019-04-07 15:59:32一重列表 1、使用set,结果会乱序 List = [3,3,2,4,4,5] NewList = list(set(List)) print (NewList)#结果:[2, 3, 4, 5] 2、使用字典fromkeys()和keys()方法,结果会乱序 让列表作为字典的key,然后取出所有... -
python中如何用函数将列表去重
2020-12-18 17:32:05定义一个函数 def remove_element(m_list):,将列表[10, 1, 2, 20, 10, 3, 2, 1, 15, 20, 44, 56, 3, 2, 1]去除重复元素 编写函数,检查传入列表的长度,如果大于2,那么仅仅保留前两个长度的内容,并将新... -
Python列表去重方法
2021-09-14 11:17:34[root@localhost ~]# python listuniq.py [2, 3, 41, 4] 方法四: 使用列表推导来去重 >>> lst1=[2,3,41,2,4,3,4] >>> temp = [] >>> [temp.append(i) for i in lst1 if not i in temp] [None, None, None, None] >>... -
Python列表去重的六种方法
2020-10-28 15:12:10如果要删除列表列表中的重复项,则同样可以用下面的几种方法来处理 >>> # 方法一: >>> data = [2, 1, 3, 4, 1] >>> [item for item in data if data.count(item) == 1] [2, 3, 4] >>> # 方法二: >>> data = [2, 1, ... -
Python列表去重如何实现?列表去重的4种方式
2021-01-29 12:19:54在开发中对列表去重是非常常见的需求,列表去重也是Python中一种常见的处理方式。列表作为Python中最常用的数据结构,承担了Python中大多数的数据存储任务,但Python本身是不满足互异性的,意思就是有可能重复,那就... -
Python实现嵌套列表去重方法示例
2021-01-20 05:11:03发现问题 python嵌套列表大家应该都不陌生,但最近遇到了一个问题,这是工作中遇到的一个坑,首先看一下问题 raw_list = [[百度, CPY], [京东, CPY], [黄轩,...正常Python去重都是使用set,所以我这边也是用这种思想处 -
Python列表去重的几种方法
2018-04-07 19:12:48工作中,面试中经常会碰到列表去重的问题,有必要总结下:方法一: 使用内置set方法来去重>>> lst1 = [2, 1, 3, 4, 1] >>> lst2 = list(set(lst1)) >>&... -
Python字符串操作常用函数
2018-08-16 16:06:29Python字符串操作常用函数,包含了检索、统计、分割、替换、大小写转换、对齐,空格删除、字符串判断(头尾+组成) -
python两种列表元素去重函数性能比较
2017-08-26 19:27:19测试函数: 第一种:list的set函数 第二种:{}.fromkeys().keys() .../usr/bin/python #-*- coding:utf-8 -*- import time import random l1 = [] leng = 10L for i in range(0,leng): temp = random.randint(1,10)