English 中文(简体)
(正文)
原标题:Remove duplicates from a list (in vim)

我的清单是:

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

我要知道,我如何能够从名单上删除这些重复之处。

Would it be also possible when I add an item to the list to check if the item is already in the list (to avoid duplicates?)

最佳回答

提 出

let list=[ 02 ,  03 ,  03 ,  16 ,  17 ,  17 ,  28 ,  29 ,  29 ]
let unduplist=filter(copy(list),  index(list, v:val, v:key+1)==-1 )

。 关于第二个问题,见:h Index()

如果是,

  1. all list items are strings;
  2. there is no empty strings possible;
  3. you don t care about order of list items

那么,你或许应当使用假肢:对于大量探照相机,速度较快(实际上不需要)。

问题回答

这种排位外法(即,在<代码>后生效:)的取代将消除复制件(条件是这些复制件是连续的):

s/( [0-9]+ ),s+1/1/g

您还可以将名单转换为一位理论家的钥匙:

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)

这既符合分类清单,也符合未经编辑的名单。

Vim 7.4.218 (25 Mar 2014) and newer

规定

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()|.




相关问题
Autoupdate VIM Plugins?

Is it possible to update vim plugins automatically?

how to unindent in vim without leaving edit mode?

I m writing a lot of python code recently, and i used the tab-to-space mode in vim. I was just wondering how would i unindent in vim without leaving edit mode for example after i finished if...: block....

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

Vim - Deleting XML Comments

How do I delete comments in XML? If the opening and the closing comment tags are on the same line, I use :g/^<!--.*-->$/d to delete the comment. How to delete the comments that are spread ...

Limiting a match in vim to certain filetypes?

I have the following in my .vimrc to highlight lines longer than 80 chars: highlight OverLength ctermbg=red ctermfg=white guibg=#592929 match OverLength /\%81v.*/ This works quite well. However, the ...

Profiling Vim startup time

I’ve got a lot of plugins enabled when using Vim – I have collected plugins over the years. I’m a bit fed up with how long Vim takes to start now, so I’d like to profile its startup and see which of ...

热门标签