Python-删除列表中重复元素的方法
1.set()方法
x = [1,2,3,4,5,1]
y = list(set(x))
print(y)
```
[1, 2, 3, 4, 5]
```
2.
x = ['b','c','d','b','c','a','a']
y = {}.fromkeys(x).keys()
print(y)
```
dict_keys(['b', 'c', 'd', 'a'])
```
3. set() 方法会改变列表顺序
z = list(set(x))
print(z)
```
['d', 'a', 'c', 'b']
```
4. 使用sort(),使之与原来的相同
z.sort(key=x.index)
z
```
['b', 'c', 'd', 'a']
```
5.
l = []
for i in x:
if not i in l:
l.append(i)
print(l)
```
['b', 'c', 'd', 'a']
Python-删除列表中重复元素的方法的更多相关文章
- python去除列表中重复元素的方法
列表中元素位置的索引用的是L.index 本文实例讲述了Python去除列表中重复元素的方法.分享给大家供大家参考.具体如下: 比较容易记忆的是用内置的set 1 2 3 l1 = ['b','c', ...
- 删除列表中重复元素以及求list中元素个数
Python 去除列表中重复的元素 来自比较容易记忆的是用内置的set l1 = ['b','c','d','b','c','a','a'] l2 = list(set(l1)) print l2 还 ...
- 【381】python 获取列表中重复元素的索引值
参考:获取python的list中含有重复值的index方法_python_脚本之家 核心思想:建立字典,遍历列表,把列表中每个元素和其索引添加到字典里面 cc = [1, 2, 3, 2, 4] f ...
- python——删除列表中的元素
在python中,删除列表元素的方法有三种,分别为remove(),del(),pop()函数 (1)remove() >>> name = ['小明','小华','小红','小李' ...
- Python 统计列表中重复元素的个数并返回其索引值
需求:统计列表list1中元素3的个数,并返回每个元素的索引 list1 = [3, 3, 8, 9, 2, 10, 6, 2, 8, 3, 4, 5, 5, 4, 1, 5, 9, 7, 10, 2 ...
- python 删除list中重复元素
list = [1,1,3,4,6,3,7] 1. for s in list: if list.count(s) >1: list.remove(s) 2. list2=[] for s in ...
- Python 去除列表中重复的元素
Python 去除列表中重复的元素 来自比较容易记忆的是用内置的set l1 = ['b','c','d','b','c','a','a'] l2 = list(set(l1)) print l2 还 ...
- Python删除列表中元素
Python中列表(list)是很常用的数据结构,删除列表中的元素有几种方法 列表的remove方法 lst = [1, 1, 3, 4] lst.remove(1) # lst->[1, 3, ...
- Java 删除ArrayList中重复元素,保持顺序
// 删除ArrayList中重复元素,保持顺序 public static List<Map<String, Object>> removeDuplicat ...
随机推荐
- strstr and strpos
啥也不说 直接上代码: <?php $email = 'name@example.com'; $domain = strstr($email, '@'); echo $domain; // 打 ...
- 633. Sum of Square Numbers 是否由两个完全平方数构成
[抄题]: Given a non-negative integer c, your task is to decide whether there're two integers a and b s ...
- 643. Maximum Average Subarray I 最大子数组的平均值
[抄题]: Given an array consisting of n integers, find the contiguous subarray of given length k that h ...
- Spring.Web.Mvc 注入(控制器属性注入)
1.web.config配置 <?xml version="1.0" encoding="utf-8"?><!-- 有关如何配置 ASP.NE ...
- SQL语句性能分析常用命令
DBCC freeproccache DBCC dropcleanbuffers 1.set statistics IO {ON| OFF} /*Transact-SQL 语句生成的磁盘活动量的信息* ...
- 用JQuery获取输入框中的光标位置
(function ($, undefined) { $.fn.getCursorPosition = function () { var el = $(this).get(0); var pos = ...
- plsql中的执行体
在plsql中的sql windows窗口中,可以编写一段执行体来达到一定的目的,类似于写一段程序,可有逻辑判断. 大概的格式为 declare ----定义变量 begin ----- 执行体: e ...
- Discrete cosine transform(离散余弦转换)
A discrete cosine transform (DCT) expresses a finite sequence of data points in terms of a sum of co ...
- ParameterizedType的作用
public interface ParameterizedType extends Type subParam.Java package com.example.test; public clas ...
- 最近做手机端,GPS,微信QQ分享总结的问题
Android端 百度地图: 1.libs包中armeabi下liblocSDK4d.so文件丢失,导致百度定位失效. 微信分享: 1.分享App,app的内容(图片加描述)不能超过32kb ,不然无 ...