Getting attribute error: 'map' object has no attribute 'sort'
Getting attribute error: 'map' object has no attribute 'sort'
我正在尝试按递增顺序对数组进行排序。但是代码出现以下错误:
a=[]
a=map(int, input().split(' '))
a.sort()
print (a)
帮帮我...
ERROR : AttributeError: 'map' object has no attribute 'sort'
在 python 3 map
中没有 return 列表。相反,它 return 是一个迭代器对象,因为 sort
是 list
对象的一个属性,所以你会得到一个属性错误。
如果要对结果进行就地排序,需要先将其转换为列表(不推荐)。
a = list(map(int, input().split(' ')))
a.sort()
但是,作为一种更好的方法,您可以使用 sorted
接受可迭代和 return 排序列表的函数,然后将结果重新分配给原始名称(推荐):
a = sorted(map(int, input().split(' ')))
我正在尝试按递增顺序对数组进行排序。但是代码出现以下错误:
a=[]
a=map(int, input().split(' '))
a.sort()
print (a)
帮帮我...
ERROR : AttributeError: 'map' object has no attribute 'sort'
在 python 3 map
中没有 return 列表。相反,它 return 是一个迭代器对象,因为 sort
是 list
对象的一个属性,所以你会得到一个属性错误。
如果要对结果进行就地排序,需要先将其转换为列表(不推荐)。
a = list(map(int, input().split(' ')))
a.sort()
但是,作为一种更好的方法,您可以使用 sorted
接受可迭代和 return 排序列表的函数,然后将结果重新分配给原始名称(推荐):
a = sorted(map(int, input().split(' ')))