1.通过set 函数进行实现,非常简单。
- #!/usr/bin/python
- list_string = ['123','abc','abc','cde']
- list_string = list(set(list_string))
- print list_string
输出的list就是去掉重复的结果
2.自己手写了一个,其实跟这个我觉得也没啥特别的用处。。娱乐
- #!/usr/bin/python
- # Filename: DeleteRep
- # use: to delete the repetation of the string
- StringList=['123','abc','abc','cde','cde']
- index = 1
- compare = 0
- length = len(StringList) - 1
- while index <= length:
- if cmp(StringList[index],StringList[compare]) == 0:
- del StringList[index]
- length -= 1 #每次del之后List长度都减少
- if index == length + 1: #限制最后出现末尾两个是重复的现象的终止
- break
- else:
- index += 1
- compare += 1
- print StringList
阅读(15428) | 评论(0) | 转发(0) |