【问题标题】:How do I return a chart from a list?如何从列表中返回图表?
【发布时间】:2021-04-20 01:49:34
【问题描述】:

此函数tickets_ages() 计算每个年龄组购买的活动门票数量。

我需要创建一个可视化表示来显示每个年龄段的门票销售情况,但我不知道该怎么做。我已经完成了基本功能,该函数只输出每个年龄段+售出的门票数量。

计算:函数取最大数,在本例中为 18,然后除以 4,即 4.5。 4.5 将代表图表上的一个刻度线*

对于其余数字,每个数字除以 4.5,与最大除法因子 18 相同。

0 to 20: 12 ---> becomes 2.66
20 to 40: 15 ---> becomes 3.33
40 to 60: 16 ---> becomes 3.55
60 to 80: 15 ---> becomes 3.33
80 and over: 18 ---> 4.5

这是我的代码:

def sort_list(value):
    sorted_v = list(set(value)) #This just puts the values once so they dont repeat, dont worry about it
    sorted_v.sort()
    return sorted_v


'''
The list
'''
ages = ['0 to 20', '0 to 20', '0 to 20', '0 to 20','0 to 20', '0 to 20', '0 to 20', '0 to 20','0 to 20', '0 to 20', '0 to 20', '0 to 20', '20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','20 to 40','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','40 to 60','60 to 80','60 to 80','60 to 80','60 to 80','60 to 80','60 to 80', '40 to 60','60 to 80','60 to 80', '80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','80 and over','40 to 60','60 to 80','60 to 80','60 to 80','60 to 80','60 to 80','60 to 80', '40 to 60','60 to 80','80 and over'] 

'''
Main function
'''
def tickets_ages(ages):
    sort_thelist = sort_list(ages)
    string_s = ''
    for source in sort_thelist:
        output = ages.count(source)        
        string_s += '{}: {} \n'.format(source, output)
    return string_s    

'''
Test code
'''

print(tickets_ages(ages))

输出应该是:

0 to 20: ***  #---> 2.66
20 to 40: *** #---> 3.33
40 to 60: *** #---> 3.55
60 to 80: *** #---> 3.33
80 and over: ***** #---> 4.5

【问题讨论】:

  • 提示:'*' * 10'**********'
  • @martineau 是的,我知道,我的主要问题是我不知道如何专门针对每个特定年龄组执行此操作,并获得最大数量的年龄组,将其除以 4 和剩下的 4.5,如果你能给我一个关于如何做的提示,那就太好了!
  • 我是在暗示要回答名义上的问题——如何制作图表。要做其他事情,我建议将每个年龄段的数字存储在某种类型的容器中,比如列表或字典.然后,您可以轻松地在评论中完成您现在询问的所有其他事情。 Python 的内置容器数据结构是它的主要优势之一——尝试更好地利用它们。

标签: python python-3.x


【解决方案1】:

根据经验,如果您打算进一步处理函数,请不要从函数返回格式化输出。修改您的函数以返回原始结果:

def tickets_ages(ages):
    sort_thelist = sort_list(ages)
    output = []
    for source in sort_thelist:
        output.append((source, ages.count(source)))
    return output

然后处理输出以计算“tick”值(即您的示例中的 4.5):

results = tickets_ages(ages)
tickets = list(zip(*results))[1]
tick = max(tickets) / 4

您现在可以使用以下信息打印结果:

for age, ticket in results:
    score = round(ticket / tick, 2)
    stars = '*' * round(score)
    print(f"{age}: {score}: {stars}")
print()

这应该打印出来:

0 to 20: 2.67: ***
20 to 40: 3.33: ***
40 to 60: 3.56: ****
60 to 80: 3.33: ***
80 and over: 4.0: ****

score 值用于演示目的,如果不需要,可以将其从 f 字符串中删除。

编辑:如果您想修改原始函数以返回图表:

def tickets_ages(ages):
    sort_thelist = sort_list(ages)
    results = []
    output = ""
    for source in sort_thelist:
        results.append((source, ages.count(source)))
    tickets = list(zip(*results))[1]
    tick = max(tickets) / 4
    for age, ticket in results:
        score = round(ticket / tick, 2)
        stars = '*' * round(score)
        output += f"{age}: {score}: {stars}\n"
    return output

print(tickets_ages(ages))

【讨论】:

  • 我建议您将其设为一个单独的函数(例如generate_chart)并将第一个函数的输出提供给新函数。
  • 是的,您可以简单地附加到字符串而不是使用打印语句(就像您在原始代码中所做的那样)并在一个函数中执行此操作,即使这不是最佳实践。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-02
  • 2015-04-03
  • 1970-01-01
相关资源
最近更新 更多