This is my list:
['02', '03', '03', '16', '17', '17', '28', '29', '29']
I would like to know how I can remove the duplicates from this list.
Would it be also possibl开发者_如何转开发e when I add an item to the list to check if the item is already in the list (to avoid duplicates?)
Try
let list=['02', '03', '03', '16', '17', '17', '28', '29', '29']
let unduplist=filter(copy(list), 'index(list, v:val, v:key+1)==-1')
. For the second question, see :h index()
.
By the way, if
- all list items are strings;
- there is no empty strings possible;
- you don't care about order of list items
then you should probably use a Dictionary instead: for large number of strings searching for duplicates is faster (and really not required).
This ex-mode (i.e. enter after :
) regex substitution will eliminate the duplicates (provided they are consecutive):
s/\('[0-9]\+'\),\s\+\1/\1/g
You could also convert the list to the keys in a Dictionary:
let list=['02', '03', '03', '16', '17', '17', '28', '29', '29']
let dict = {}
for l in list
let dict[l] = ''
endfor
let uniqueList = keys(dict)
This works for both sorted and unsorted lists.
Vim 7.4.218 (25 Mar 2014) and newer
provides
uniq({list} [, {func} [, {dict}]]) *uniq()* *E882*
Remove second and succeeding copies of repeated adjacent
{list} items in-place. Returns {list}. If you want a list
to remain unmodified make a copy first: >
:let newlist = uniq(copy(mylist))
< The default compare function uses the string representation of
each item. For the use of {func} and {dict} see |sort()|.
精彩评论