- 论坛徽章:
- 0
|
我想从list中根据index删除只出现1次的elements- #!/usr/bin/python
- def list_filter(data):
- target_index = []
- #find out the indices of the elements to delete
- for i in xrange(len(data)):
- if data.count(data[i]) == 1:
- target_index.append(i)
- print target_index
- #delete
- for j in target_index:
- del data[j]
- return data
- print list_filter([1,2,3,4,2])
复制代码 运行出错如下,不知如何解决?
./test.py
[0, 2, 3]
Traceback (most recent call last):
File "./test.py", line 17, in <module>
print list_filter([1,2,3,4,2])
File "./test.py", line 13, in list_filter
del data[j]
IndexError: list assignment index out of range
|
|