python-3.x 为什么输出〈0x00000处的Map对象...>和〈0x00000处的过滤器对象...>

ergxz8rk  于 2023-02-17  发布在  Python
关注(0)|答案(2)|浏览(133)
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),)

因此,当我尝试运行这个程序时,我将面临这个问题,输出将显示〈Map对象在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]
oiopk7p5

oiopk7p51#

像这样改变你的功能,

#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返回Map对象和filter返回过滤器对象,因此将它们转换为列表。

dbf7pr2w

dbf7pr2w2#

你可以使用列表解析,它更简洁,例如:

items = [
    ("name", 13),
    ("age", 12)

]

prices = [item[1] for item in items]
print(prices)

相关问题