脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python实现字典去除重复的方法示例

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

2020-11-29 00:34无法自拔 Python

这篇文章主要介绍了Python实现字典去除重复的方法,涉及Python字典遍历、文件读取、去除重复等相关操作技巧,需要的朋友可以参考下

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/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 等等

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

延伸 · 阅读

精彩推荐