列表去重
输入一个列表,去掉列表中重复的数字,按原来次序输出!
输入格式:
在一行中输入列表
输出格式:
在一行中输出不重复列表元素
输入样例:
在这里给出一组输入。例如:
[4,7,5,6,8,6,9,5]
输出样例:
在这里给出相应的输出。例如:
4 7 5 6 8 9
思路:
存入列表,用set去重。
代码如下:
lst = input()
lst = lst.replace('[','')
lst = lst.replace(']','')
lst = lst.replace(' ','')
lst = lst.split(',')
res = list(set(lst))
res.sort(key = lst.index)
print(" ".join(res))
改进代码:
使用eval()函数处理列表格式输入。
lst1 = eval(input())
lst2 = list(set(lst1))
lst2.sort(key = lst1.index)
print(' '.join(list(map(str, lst2))))