[英]remove a repeated number in a list of numbers in python
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]
有一个输入,如:
[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输出最多为:
[12,34,9]
不能使用 2, 22, 25,12 因为前 12 个中有 2
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.我已经做到了
[12,2,34,25,9]
,但我无法删除重复的数字。 I have tried to separate each number to compare between each other, but i have not managed to do it.我试图将每个数字分开以相互比较,但我还没有做到。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.