1

I have seen some similar questions but not exactly what im asking, so: there cannot be a repeated digit in any of the other elements of the list, and if there's one repeated, only one must appear.

there's an input like: [12,22,12,2,34,25,9]

the output most be: [12,34,9] the 2, 22, 25,12 cannot be used because the first 12 have a 2 in it

I have made it to the point that i have [12,2,34,25,9], but I'm not able to remove the repeated numbers. I have tried to separate each number to compare between each other, but i have not managed to do it.

  • Hint: Something like `collections.Counter` could be of use here. Are you trying to keep only the elements which appear more than once? I want to make sure I understand things correctly. – AMC Mar 15 '20 at 21:37
  • What’s wrong with 22, 2, 25 that they don’t appear in your required output? – quamrana Mar 15 '20 at 21:38
  • https://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-whilst-preserving-order – Christian Sloper Mar 15 '20 at 21:39
  • Your question, as stated, has several duplicates in existence. However, on looking at your example, the given output does not match your description. It appears that you may need to remove any number that contains a *digit* that has already been used. If so, your question is too broad: Stack Overflow is not a coding or tutorial site. Follow the posting guidelines: post your honest attempt at a solution. See [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). "I can't do it" is not a Stack Overflow issue. – Prune Mar 15 '20 at 21:44

1 Answers1

2

Sets cannot have repeating entries so

l = [12,22,12,2,34,25,9]
l = list(set(l))

will produce the desired result.

EDIT:

def no_rep_digit(l):
    l_new = []
    digits = []
    for num in l:
        if not any([(digit in digits) for digit in str(num)]):
            l_new.append(num)
            digits.extend([digit for digit in str(num)])
        else:
            digits.extend([digit for digit in str(num)])
    return l_new

l = no_rep_digit(l)
Clade
  • 966
  • 1
  • 6
  • 14