Remove duplicates from list (in vim)

This is my list:

['02', '03', '03', '16', '17', '17', '28', '29', '29'] 

I would like to know how I can remove duplicates from this list.

It would also be possible when I add an item to the list to check if the item is already in the list (to avoid duplication?)

+6
source share
3 answers

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;
  • no empty lines;
  • You do not need the order of the list items.

then you should probably use a dictionary instead: for a large number of lines, duplicate searches are faster (and really not required).

+13
source

This experimental (i.e., enter after : replacement of regular expressions eliminates duplicates (if they are consecutive):

 s/\('[0-9]\+'\),\s\+\1/\1/g 
+3
source

You can also convert the list to 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.

+2
source

Source: https://habr.com/ru/post/892360/


All Articles