Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7399321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:02:45+00:00 2026-05-29T04:02:45+00:00

The question is, how can I remove elements that appear more often than once

  • 0

The question is, how can I remove elements that appear more often than once in an array completely. Below you see an approach that is very slow when it comes to bigger arrays.
Any idea of doing this the numpy-way? Thanks in advance.

import numpy as np

count = 0
result = []
input = np.array([[1,1], [1,1], [2,3], [4,5], [1,1]]) # array with points [x, y]

# count appearance of elements with same x and y coordinate
# append to result if element appears just once

for i in input:
    for j in input:
        if (j[0] == i [0]) and (j[1] == i[1]):
            count += 1
    if count == 1:
        result.append(i)
    count = 0

print np.array(result)

UPDATE: BECAUSE OF FORMER OVERSIMPLIFICATION

Again to be clear: How can I remove elements appearing more than once concerning a certain attribute from an array/list ?? Here: list with elements of length 6, if first and second entry of every elements both appears more than once in the list, remove all concerning elements from list. Hope I’m not to confusing. Eumiro helped me a lot on this, but I don’t manage to flatten the output list as it should be 🙁

import numpy as np 
import collections

input = [[1,1,3,5,6,6],[1,1,4,4,5,6],[1,3,4,5,6,7],[3,4,6,7,7,6],[1,1,4,6,88,7],[3,3,3,3,3,3],[456,6,5,343,435,5]]

# here, from input there should be removed input[0], input[1] and input[4] because
# first and second entry appears more than once in the list, got it? :)

d = {}

for a in input:
    d.setdefault(tuple(a[:2]), []).append(a[2:])

outputDict = [list(k)+list(v) for k,v in d.iteritems() if len(v) == 1 ]

result = []

def flatten(x):
    if isinstance(x, collections.Iterable):
        return [a for i in x for a in flatten(i)]
    else:
        return [x]

# I took flatten(x) from http://stackoverflow.com/a/2158522/1132378
# And I need it, because output is a nested list :(

for i in outputDict:
    result.append(flatten(i))

print np.array(result)

So, this works, but it’s impracticable with big lists.
First I got
RuntimeError: maximum recursion depth exceeded in cmp
and after applying
sys.setrecursionlimit(10000)
I got
Segmentation fault
how could I implement Eumiros solution for big lists > 100000 elements?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:02:46+00:00Added an answer on May 29, 2026 at 4:02 am
    np.array(list(set(map(tuple, input))))
    

    returns

    array([[4, 5],
           [2, 3],
           [1, 1]])
    

    UPDATE 1: If you want to remove the [1, 1] too (because it appears more than once), you can do:

    from collections import Counter
    
    np.array([k for k, v in Counter(map(tuple, input)).iteritems() if v == 1])
    

    returns

    array([[4, 5],
           [2, 3]])
    

    UPDATE 2: with input=[[1,1,2], [1,1,3], [2,3,4], [4,5,5], [1,1,7]]:

    input=[[1,1,2], [1,1,3], [2,3,4], [4,5,5], [1,1,7]]
    
    d = {}
    for a in input:
        d.setdefault(tuple(a[:2]), []).append(a[2])
    

    d is now:

    {(1, 1): [2, 3, 7],
     (2, 3): [4],
     (4, 5): [5]}
    

    so we want to take all key-value pairs, that have single values and re-create the arrays:

    np.array([k+tuple(v) for k,v in d.iteritems() if len(v) == 1])
    

    returns:

    array([[4, 5, 5],
           [2, 3, 4]])
    

    UPDATE 3: For larger arrays, you can adapt my previous solution to:

    import numpy as np
    input = [[1,1,3,5,6,6],[1,1,4,4,5,6],[1,3,4,5,6,7],[3,4,6,7,7,6],[1,1,4,6,88,7],[3,3,3,3,3,3],[456,6,5,343,435,5]]
    d = {}
    for a in input:
        d.setdefault(tuple(a[:2]), []).append(a)
    np.array([v for v in d.itervalues() if len(v) == 1])
    

    returns:

    array([[[456,   6,   5, 343, 435,   5]],
           [[  1,   3,   4,   5,   6,   7]],
           [[  3,   4,   6,   7,   7,   6]],
           [[  3,   3,   3,   3,   3,   3]]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question Can I build a image database/library that has an e-commerce style checkout system
Simple question: Can a swing frame be completely modal ( block all others windows
I need to go through a set and remove elements that meet a predefined
I'm concerned about using $pop to remove elements of an array (embedded document) in
This question was inspired by this answer to another question, indicating that you can
I have read this question: Deleting array elements in JavaScript - delete vs splice
Simple question: Can I mix in my desktop application Java and JavaFX Script code?
this question can create a misunderstanding: I know I have to use CSS to
My question can be boiled down to, where does the string returned from stringstream.str().c_str()
Here is the question: Can TortoiseSvn work on Windows 2008 without turning off User

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.