如何获得在python中打印结果的函数

时间:2018-10-09 15:35:22

标签: python python-3.x

def top_quartile_prices(prices):
prices.sort()
n = int( len(prices ) / 4 )
top_list = []
    for i in range(len(prices) - 1, len(prices) - n - 1, -1):
    top_list.append(prices[i])
    top_list.sort()

prices = ['16.13', '32.2', '11.65', '39', '13.83', '25.33', '4.99', '13.1', '150', '37.81', '4.81', '4', '32.08',
          '12.66', '19.54', '48.52', '65.92', '18.2', '13.09', '19.32', '7.63', '2.23', '116.12', '3.66', '73.45',
          '54.71', '80.07', '15.99', '30.88', '10.91', '87.7', '6.34', '5.36', '20.66', '62.05', '88.98', '4.3',
          '63.42', '3.89', '34.01', '28.42', '4.69', '15.3', '55.22', '43.48', '11.73', '167.05', '11.17', '18.84',
          '44.31', '19.38', '29.38', '21.84', '57.59', '41.42', '23.91', '145.28', '14.76', '75.5', '2.32',
          '112.19', '38.87', '55.61', '13.35', '27.4', '6.49', '40.94', '8.66', '6.59', '45.73', '34.53', '8.47',
          '71.03', '108.39', '37.06']
top_quartile_prices(prices)
print('Top top quartile prices are :', top_quartile_prices(prices))

我的函数不会显示前四分位数

4 个答案:

答案 0 :(得分:0)

top_quartile_prices函数中,您没有返回正在计算的值

print('Top top quartile prices are :', top_quartile_prices(prices))

仅打印None

答案 1 :(得分:0)

def top_quartile_prices(prices):
    prices.sort()
    n = int( len(prices ) / 4 )
    top_list = []
    for i in range(len(prices) - 1, len(prices) - n - 1, -1):
        top_list.append(prices[i])
        top_list.sort()
    return top_list

prices = ['16.13', '32.2', '11.65', '39', '13.83', '25.33', '4.99', '13.1', '150', '37.81', '4.81', '4', '32.08',
          '12.66', '19.54', '48.52', '65.92', '18.2', '13.09', '19.32', '7.63', '2.23', '116.12', '3.66', '73.45',
          '54.71', '80.07', '15.99', '30.88', '10.91', '87.7', '6.34', '5.36', '20.66', '62.05', '88.98', '4.3',
          '63.42', '3.89', '34.01', '28.42', '4.69', '15.3', '55.22', '43.48', '11.73', '167.05', '11.17', '18.84',
          '44.31', '19.38', '29.38', '21.84', '57.59', '41.42', '23.91', '145.28', '14.76', '75.5', '2.32',
          '112.19', '38.87', '55.61', '13.35', '27.4', '6.49', '40.94', '8.66', '6.59', '45.73', '34.53', '8.47',
          '71.03', '108.39', '37.06']

top_quartile_prices(prices)

print('Top top quartile prices are :', top_quartile_prices(prices))

答案 2 :(得分:0)

假设已在粘贴时格式化了该格式的缩进,但您的函数没有返回,包括return top_list。 同样,在打印语句之前的函数调用也是不必要的。

def top_quartile_prices(prices):
    prices.sort()
    n = int( len(prices ) / 4 )
    top_list = []
    for i in range(len(prices) - 1, len(prices) - n - 1, -1):
        top_list.append(prices[i])
        top_list.sort()
    return top_list
# Top top quartile prices are : ['55.22', '55.61', '57.59', '6.34', '6.49', '6.59', '62.05', '63.42', '65.92', '7.63', '71.03', '73.45', '75.5', '8.47', '8.66', '80.07', '87.7', '88.98']

答案 3 :(得分:0)

您在这里遇到几个问题:

1)如前所述,您不会从函数中返回top_list

2)您的价格是字符串,因此可能无法按照您期望的价格排序。将此添加到函数顶部:

prices = [float(p) for p in prices]

3)您无需遍历列表即可获得前四分位数。您可以使用列表切片,如下所示:

n = int( len(prices ) / 4 )
top_list = prices[len(prices)-n:]