去掉文件中重复的元件, 生成无重复的列表
有一个文件,内容如下
file_name1
file_name4
file_name3
file_name11
file_name7
file_name1
file_name3
file_name1
file_name8
文件里有重复的名字,现想去掉重复的,只保留一个,用 python 脚本实现,代码如下:
1 #!/usr/bin/python3
2 # compare the name and remove the duplicated one
3
4 name_file = open("text1.txt", "r").read() # convert the whole file to a string
5 name_list = name_file.splitlines() # use string.splitlines() method, split "\n", result in a list
6 name_list.sort() # use list.sort() method to sort the list
7
8 print(name_list)
9
10 for name in name_list:
11 if name_list.count(name) == 1: # use list.count(x) method to count the number of x in the list
12 pass # pass the step without do anything
13 else:
14 name_list.remove(name) # the most important step , use list.remove(x) method to remove the element x in the list
15
16 print("after ...")
17 print(name_list)
运行结果如下:
['file_name1', 'file_name1', 'file_name1', 'file_name11', 'file_name3', 'file_name3', 'file_name4', 'file_name7', 'file_name8']
after ...
['file_name1', 'file_name11', 'file_name3', 'file_name4', 'file_name7', 'file_name8']
可以看到最后输出的 list 中已经把所有重复的元件都去掉了。
虽然看上去这个脚本很简单,但是还是用到了几个不同的method, 而且编程的思想也有体现,这个未必是最简单的方法,但是很实用。