http://bbs.chinaunix.net/thread-1680208-1-1.html

如何找出 python list 中有重复的项

http://www.cnblogs.com/feisky/archive/2012/12/06/2805251.html

比较容易记忆的是用内置的set
l1 = ['b','c','d','b','c','a','a']
l2 = list(set(l1))
print l2

还有一种据说速度更快的,没测试过两者的速度差别
l1 = ['b','c','d','b','c','a','a']
l2 = {}.fromkeys(l1).keys()
print l2

这两种都有个缺点,祛除重复元素后排序变了:
['a', 'c', 'b', 'd']

如果想要保持他们原来的排序:

用list类的sort方法
l1 = ['b','c','d','b','c','a','a']
l2 = list(set(l1))
l2.sort(key=l1.index)
print l2
也可以这样写
l1 = ['b','c','d','b','c','a','a']
l2 = sorted(set(l1),key=l1.index)
print l2

也可以用遍历
l1 = ['b','c','d','b','c','a','a']
l2 = []
for i in l1:
if not i in l2:
l2.append(i)
print l2
上面的代码也可以这样写
l1 = ['b','c','d','b','c','a','a']
l2 = []
[l2.append(i) for i in l1 if not i in l2]
print l2

这样就可以保证排序不变了:
['b', 'c', 'd', 'a']

转自:http://blog.csdn.net/rainharder/article/details/5728443

 #!/usr/bin/env python
# coding: utf-8 import os
import sys
import string
import operator
import re
import threading
import csv from time import sleep,ctime
from collections import defaultdict
from collections import Counter def test_01():
#content ==> ###pos=350143600,pts=2676718###
#filename="F:\\yingc\\work\\goxceed-dvbs-hd\\6605\\solution\\aa"
filename="./aa"
pos=-1
dts=-1
poslist=[]
dtslist=[] str1="###pos="
str2=",pts=" f = open(filename)
for line in f:
aa=line[0:len(str1)]
if aa == str1:
pos=line[len(str1):line.index(str2)]
dts=line[line.index(str2)+len(str2):len(line)-3-1]
poslist.append(pos)
dtslist.append(dts)
f.close() #s=[11,22,11,44,22,33]
d = defaultdict(list)
for k,va in [(v,i) for i,v in enumerate(poslist)]:
d[k].append(va)
#print d.items()
count=0
for value in d.items():
if len(value[1])>1:
print value
count=count+1
print "poslen:"+str(len(poslist))+",dtslen"+str(len(dtslist))
print str(len(d))+","+str(count) #d = defaultdict(list)
#for k,va in [(v,i) for i,v in enumerate(dtslist)]:
# d[k].append(va)
##print d.items()
#for value in d.items():
# if len(value[1])>1:
# print value #print Counter([11,22,11,44,22,33]) if __name__ == "__main__":
test_01()
print "finish"

aa文件中的内容如:

###pos=1349796,pts=15015###
###pos=2337820,pts=27986###
###pos=2705098,pts=29988###
###pos=6660200,pts=54721###
###pos=8055314,pts=61061###
###pos=8871800,pts=65315###
###pos=9503420,pts=68401###
###pos=12855218,pts=88338###
###pos=14253082,pts=98765###
###pos=15813764,pts=109192###
###pos=15813764,pts=109192###
###pos=15813764,pts=109192###
###pos=15813764,pts=109192###
###pos=16056146,pts=110735###
###pos=16394580,pts=113988###
###pos=17011532,pts=119911###
###pos=17257542,pts=122372###
###pos=17417974,pts=124040###
###pos=17816976,pts=128169###
###pos=17993398,pts=129838###
###pos=18302190,pts=132215###
###pos=19166088,pts=139055###
###pos=19675276,pts=143059###
###pos=19994992,pts=146146###

Python list去重及找出,统计重复项的更多相关文章

  1. 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---找出网页中所有的href

    阅读OReilly.Web.Scraping.with.Python.2015.6笔记---找出网页中所有的href 1.查找以<a>开头的所有文本,然后判断href是否在<a> ...

  2. python -- 对list去重并找出列表list中的重复元素

    一.一个列表中可能含有重复元素,使用set()可以实现列表的去重处理,但是无法知道哪些元素是重复的,下面的函数用于找出哪些元素重复了,以及重复的次数. 代码: from collections imp ...

  3. Python实用黑科技——找出最大/最小的n个元素

    需求: 快速的获取一个列表中最大/最小的n个元素. 方法: 最简便的方法是使用heapq模组的两个方法nlargest()和nsmallest(),例如: In [1]: import heapqIn ...

  4. Python基础-生物信息:找出基因,生物学家使用字母A、C、T和G构成的字符串建模一个基因组。

    生物信息:找出基因,生物学家使用字母A.C.T和G构成的字符串建模一个基因组.一个基因是基因组的子串,它从三元组ATG后开始在三元组TAG.TAA或TGA之前结束.此外,基因字符串的长度是3的倍数,而 ...

  5. js中找string中重复项最多的字符个数

    // split():字符串中的方法,把字符串转成数组. // sort():数组中的排序方法,按照ACALL码进行排序. // join():数组中的方法,把数组转换为字符串 function de ...

  6. python 找出数组重复的元素

    """ 从头扫到尾,只要当前元素值与下标不同,就做一次判断,numbers[i]与numbers[numbers[i]], 相等就认为找到了重复元素,返回true,否则就 ...

  7. 【Python】使用geocoder找出本机IP所在经纬度和城市

    代码: import geocoder g = geocoder.ip('me') print(g.latlng) # 经纬度 print(g.city) # 所在城市 输出: C:\Users\ho ...

  8. python多字符中找出最大匹配(网友处学习)

    #如'abbcc','abbdd' 找到abba='abbcc'b='abbdd'from difflib import *s=SequenceMatcher(None,a,b)m=s.find_lo ...

  9. Python 脚本帮你找出微信上删除了你的“好友“

随机推荐

  1. svn迁移

    一.       VisualSVN服务器迁移 场景:把A服务器备份到B服务器 方法: 1.拷贝A上Repositories文件夹到B上 2.打开B上VisualSVN Server Manager ...

  2. C# 顺序表操作

    虽然.NET已经是现实了Reverse(),但是学习算法有必要知道其是怎么实现的: private static void ReverseArray(int[] array) { int temp; ...

  3. [转] shell字符串操作方法,以及实例

    每一种语言都有他独自的字符串操作方法,shell也一样,下面以以例子的方式,简单介绍常用方法. 1,取得字符串长度 string=abc12342341 //等号二边不要有空格 echo ${#str ...

  4. WPF解析PPT为图片

    偶遇需要解析 PPT为单张图片 其中,对于包含动画的PPT页,分别对动画最后效果进行截取,即每个连续动画截取 (动画N个)N+1(原图)张 http://git.oschina.net/jiailiu ...

  5. 玩转SmartQQ之登录

    SmartQQ是腾讯新出的一个WebQQ,登录地址是:http://w.qq.com/,目前之前的WebQQ可以继续使用,登录地址:http://web2.qq.com/webqq.html,Smar ...

  6. mysql注入绕过的一些技巧

    虽然mysql + php的开发中可以使用pdo中,但是有些老久的程序没有使用,或其他原因 1.注释绕过 select/*comment*/user/*zzsdsdsf*/from mysql.use ...

  7. 百度快收录吧!!!a39fe054b88866bc737dd5fb02f39e41

    百度快收录吧!!!a39fe054b88866bc737dd5fb02f39e41  }416oTemocleW{yek

  8. HTTP - 内容编码

    HTTP 应用程序有时在发送之前需要对内容进行编码.例如,在把很大的 HTML 文档发送给通过慢速连接上来的客户端之前,服务器可能就会对它进行压缩,这样有助于减少传输实体的时间. 内容编码过程 内容编 ...

  9. UVA 10720 Graph Construction 贪心+优先队列

    题目链接: 题目 Graph Construction Time limit: 3.000 seconds 问题描述 Graph is a collection of edges E and vert ...

  10. hihocoder #1300 : 展胜地的鲤鱼旗 dp

    题目链接: http://hihocoder.com/problemset/problem/1300 题解: 先用栈预处理出每个‘)’匹配的‘(’的位子,放在pos数组中. dp[i]表示以i结尾的合 ...