如何在单个列表中获得结果?

时间:2016-05-15 12:06:22

标签: python python-3.x

我正在根据Codefight Code挑战创建票价估算工具。

这可以告诉您在请求之前您的乘车费用是多少。它的工作原理是通过这个公式

传递近似的行驶距离和行驶时间

(Cost per minute) * (ride time) + (Cost per mile) * (ride distance)

示例

  • ride_time = 30,
  • ride_distance = 7,
  • cost_per_minute = [0.2,0.35,0.4,0.45]
  • cost_per_mile = [1.1,1.8,2.3,3.5],输出
  • fareEstimator (ride_time,ride_distance,cost_per_minute, cost_per_mile)= [13.7,23.1,28.1,38]

输出必须在列表中

自:

  • 30 * 0.2 + 7 * 1.1 = 6 + 7.7 = 13.7
  • 30 * 0.35 + 7 * 1.8 = 10.5 + 12.6 = 23.1
  • 30 * 0.4 + 7 * 2.3 = 12 + 16.1 = 28.1
  • 30 * 0.45 + 7 * 3.5 = 13.5 + 24.5 = 38

这是我的代码:

def fareEstimator(ride_time, ride_distance, cost_per_minute, cost_per_mile):

    for cpm, cpmile in zip(cost_per_minute, cost_per_mile):
        result = round(( ride_time * cpm ) + ( ride_distance * cpmile ), 1)
        print([result])

fareEstimator(30, 7, [0.2, 0.35, 0.4, 0.45], [1.1, 1.8, 2.3, 3.5])

其输出

[13.7]
[23.1]
[28.1]
[38.0]

我尝试使用列表理解,我得到了TypeError: 'float' object is not iterable

现在请帮我把结果放在一个列表中。就像这样。

[13.7,23.1,28.1,38]

4 个答案:

答案 0 :(得分:3)

不是打印,而是附加到您返回的列表:

result = []
for cpm, cpmile in zip(cost_per_minute, cost_per_mile):
    value = round(ride_time * cpm + ride_distance * cpmile, 1)
    result.append(value)
return result

这很容易转换为列表理解;只需将result表达式放在前面:

return [round(ride_time * cpm + ride_distance * cpmile, 1)
        for cpm, cpmile in zip(cost_per_minute, cost_per_mile)]

演示:

>>> def fareEstimator(ride_time, ride_distance, cost_per_minute, cost_per_mile):
...     return [round(ride_time * cpm + ride_distance * cpmile, 1)
...             for cpm, cpmile in zip(cost_per_minute, cost_per_mile)]
...
>>> fareEstimator(30, 7, [0.2, 0.35, 0.4, 0.45], [1.1, 1.8, 2.3, 3.5])
[13.7, 23.1, 28.1, 38.0]

请注意,此返回新列表,如果您仍需要打印此结果,请在调用fareEstimator()的位置执行此操作。

答案 1 :(得分:1)

最简单的(编辑方式)方法是将print([result])替换为yield result,而fareEstimator将成为a generator函数。

如果您需要一个列表(例如,用于索引访问)而不仅仅是一些可迭代的,请按以下方式调用它:

print(list(fareEstimator(...)))

或者,如果您知道总是需要一个列表,您也可以这样做:

def fareEstimator(ride_time, ride_distance, cost_per_minute, cost_per_mile):
    result = []
    for cpm, cpmile in zip(cost_per_minute, cost_per_mile):
        value = round((ride_time * cpm) + (ride_distance * cpmile), 1)
        result.append(value)
    return result

或者,更好,与list comprehension

相同
def fareEstimator(ride_time, ride_distance, cost_per_minute, cost_per_mile):
    return [round((ride_time * cpm) + (ride_distance * cpmile), 1)
            for cpm, cpmile in zip(cost_per_minute, cost_per_mile)]

print(fareEstimator(...))

会打印该列表。

答案 2 :(得分:1)

问题在于:

print([result])

对于每个结果,您创建一个仅包含结果的列表,然后打印它。 您想要做的似乎是创建一个空列表,并将每个结果添加到它。

像这样:

def fareEstimator(ride_time, ride_distance, cost_per_minute, cost_per_mile):
    results = []
    for cpm, cpmile in zip(cost_per_minute, cost_per_mile):
        result = round(( ride_time * cpm ) + ( ride_distance * cpmile ), 1)
        results.append(result)
    print(results)

请参阅https://docs.python.org/3/tutorial/datastructures.html以了解有关您可以在列表中执行的操作的更多信息。

答案 3 :(得分:1)

您只需要使用列表推导并返回函数的结果:

def fareEstimator(ride_time, ride_distance, cost_per_minute, cost_per_mile):    
    return [round(( ride_time * cpm ) + ( ride_distance * cpmile ), 1) 
            for cpm, cpmile in zip(cost_per_minute, cost_per_mile)]

>>> fareEstimator(30, 7, [0.2, 0.35, 0.4, 0.45], [1.1, 1.8, 2.3, 3.5])
[13.7, 23.1, 28.1, 38.0]