【问题标题】:Why do I get output <map object at 0x00000...> and <filter object at 0x00000....>为什么我得到输出 <map object at 0x00000...> 和 <filter object at 0x00000....>
【发布时间】:2023-02-15 08:40:28
【问题描述】:
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 object at 0x00000...> 和 <filter object at 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]

【问题讨论】:

标签: python python-3.x


【解决方案1】:

像这样改变你的功能,

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

【讨论】:

    【解决方案2】:

    您可以使用 List Comprehensions,它更简洁,例如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      • 2019-04-14
      • 1970-01-01
      • 2012-09-01
      • 2014-06-17
      • 2018-04-22
      相关资源
      最近更新 更多