为什么我在0x00000...>获得输出<map对象,在0x00000...<过滤对象>


import sys
#Multiply the value in the list based on the selected value of s
def scale(l, s):
return map(lambda x: x * s, l)
#Sort the value based on the last digit value
def sort(l):
return sorted(l, key=lambda x: x % 10)
#Output number if is greater than average total
def goodSales(l):
return filter(lambda x: x > sum(l) / len(l), l)

seq = sys.argv[1]
sca = sys.argv[2]
seq = [int(x) for x in seq.split(',')]
sca = int(sca)

print('The scaled number is:', scale(seq, sca),
'The sorted sales numbers are:', sort(seq),
'The good sales numbers are:', goodSales(seq),)

所以当我试图运行这个程序时,我将面临这个问题,输出将显示<映射0x00000处的对象…>并且<在0x00000处筛选对象……>。我真的不确定出了什么问题,有人能给我一些建议吗。

输入

python sales.py 10,20,30,40,50,60 2

预期输出

The scaled number is: [20, 40, 60, 80, 100, 120] The sorted sales
numbers are: [10, 20, 30, 40, 50, 60] The good sales numbers are:
[40, 50, 60]

像这样更改您的函数,

#Multiply the value in the list based on the selected value of s
def scale(l, s):
return list(map(lambda x: x * s, l))
#Output number if is greater than average total
def goodSales(l):
return list(filter(lambda x: x > sum(l) / len(l), l))

map返回映射对象,filter返回过滤器对象,因此将它们转换为列表。

您可以使用列表理解,它更干净,例如:

items = [
("name", 13),
("age", 12)
]
prices = [item[1] for item in items]
print(prices)

相关内容

  • 没有找到相关文章

最新更新