What does this: s[s[1:] == s[:-1]] do in numpy?

前端 未结 4 637

I\'ve been looking for a way to efficiently check for duplicates in a numpy array and stumbled upon a question that contained an answer using this code.

What does th

4条回答
  •  情书的邮戳
    2020-12-30 03:29

    s[1:] == s[:-1] compares s without the first element with s without the last element, i.e. 0th with 1st, 1st with 2nd etc, giving you an array of len(s) - 1 boolean elements. s[boolarray] will select only those elements from s which have True at the corresponding place in boolarray. Thus, the code extracts all elements that are equal to the next element.

提交回复
热议问题