首页 > 解决方案 > Python - gap on exit sequence,error on comparing decimal

问题描述

I enter with numbers out of sequence, subtract the repeated ones, maintaining the order, subtract[']"of list on exit (is there any more pythonic way for this?). I do not know where I'm going wrong, it fails to compare units of the dozen and a space appears on the output.

a = 1 1 4 4 4 8 8 2 14 14 11 11

expected exit b = 1 4 8 2 14 11

wrong output b = 1 4 8 2

def repeated(s):
    t = []
    [t.append(item) for item in s if not t.count(item)]
    return t

def remove(s,to_remove):
    for x in to_remove:
        s = s.replace(x, '')
    return s

def main():
    a = input('a = ')
    print('b = ', (remove(str(repeated(a)), "['],")))

main()
exit()

标签: python

解决方案


more better use sets

a = set(input().split()) 
print(' '.join(a))

sets can be contains only unique values, and you don't need to remove values. All values will be unique by default.


推荐阅读