有两个要求:重复出现的数字变成一个
递增排序
例如--> 输入 1 1 1 4 5 6 3
输出 1 3 4 5 6
以下是我写的代码,按以上要求做出来了,但是结果确是 ‘map’ 类型,怎么让结果改成 ‘int’ 类型呢?
num = list(map(int, input().split()))
num1 = []
for i in num:
if num.count(i) > 1:
break
num1.append(i)
while i <= len(num):
num1.append(int(num.pop()))
i += 1
num1.sort()
num1 = map(int,num1)
print((' ').join(str(x) for x in num1))
print(type(num1))
第14行用list()将map类型转换成列表类型,改成如下即可:
num1 = list(map(int,num1))
运行结果:
F:\2021\qa\ot2>t5
1 1 1 4 5 6 3
1 3 4 5 6
<class 'list'>
如有帮助,请点击下本回答的采纳按钮。
map()返回的是一个迭代对象。要用list()转成列表,
而且你的代码有一些问题, 去除重复用set(num)就可以了
num = list(map(int, input().split()))
num1 = list(set(num))
num1.sort()
print((' ').join(str(x) for x in num1))
如有帮助,望采纳!谢谢!