Python实现字典去除重复的方法示例

yipeiwu_com6年前Python基础

本文实例讲述了Python实现字典去除重复的方法。分享给大家供大家参考,具体如下:

#!/usr/bin/env python
# encoding: utf-8
#字典去重小代码
import sys
import os
import platform
try:
  pass 
except:
  print '''you have something wrong this is a simple jiaoben '''
  sys.exit()
why = 'why.txt'
for i in xrange(len(sys.argv)):
  if(i>=1):
    other = sys.argv[i]
    if os.path.exists(other):
      pass
    else:
      print other + ' file not find'
      sys.exit()
    if 'Windows' in platform.system():
      os.system("type "+other+" >> "+why)
    else:
      os.system("cat "+other+" >> "+why)
yuan = open('why.txt','r')
dirc = open('whynot.txt','w')
for line in set(yuan.readlines()):
  if line == '' or line == '\r\n':
    pass
  else:
    dirc.writelines(line)

很简单的思路 把文件放入到why.txt中 然后再去重得到whynot.txt
Usage: quchong.py 1.txt 2.txt 3.txt 等等

PS:本站还有两款比较简单实用的在线文本去重复工具,推荐给大家使用:

在线去除重复项工具:
http://tools.jb51.net/code/quchong

在线文本去重复工具:
http://tools.jb51.net/aideddesign/txt_quchong

更多关于Python相关内容可查看本站专题:《Python字典操作技巧汇总》、《Python字符串操作技巧汇总》、《Python常用遍历技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

pytorch 固定部分参数训练的方法

pytorch 固定部分参数训练的方法

需要自己过滤 optimizer.SGD(filter(lambda p: p.requires_grad, model.parameters()), lr=1e-3) 另外,如...

横向对比分析Python解析XML的四种方式

横向对比分析Python解析XML的四种方式

在最初学习PYTHON的时候,只知道有DOM和SAX两种解析方法,但是其效率都不够理想,由于需要处理的文件数量太大,这两种方式耗时太高无法接受。 在网络搜索后发现,目前应用比较广泛,且效...

pytorch 归一化与反归一化实例

ToTensor中就有转到0-1之间了。 # -*- coding:utf-8 -*- import time import torch from torchvisi...

Python使用googletrans报错的解决方法

Python使用googletrans报错的解决方法

问题 最近在工作中发现了一个问题,Python代码一直用着免费的Google翻译API插件googletrans,这两天突然就报错了: Traceback (most recen...

Python+Selenium+PIL+Tesseract自动识别验证码进行一键登录

Python+Selenium+PIL+Tesseract自动识别验证码进行一键登录

本文介绍了Python+Selenium+PIL+Tesseract自动识别验证码进行一键登录,分享给大家,具体如下: Python 2.7 IDE Pycharm 5.0.3...