简体   繁体   English

如何获得一个函数来返回python中表的每一行?

[英]How can I get a function to return each row of a table in python?

I am building a simple function which calls in a list of tuples and returns a string in the form of a table.我正在构建一个简单的函数,它调用一个元组列表并以表格的形式返回一个字符串。 I am not sure how I can generate each new row of a table.我不确定如何生成表格的每一行。 This is something that I've been struggling with since I started coding a few months ago.这是我几个月前开始编码以来一直在努力解决的问题。

My task: Take a list of ages say [20, 33, 99, 6, 21] and generate the following table:我的任务:拿一个年龄列表说 [20, 33, 99, 6, 21] 并生成下表:

No.不。 Age年龄 Group团体
1 1 20 20 Young Adult (20-29)青年 (20-29)
2 2 33 33 Adult (30-44)成人 (30-44)
3 3 99 99 Very Old (81+)很老 (81+)
4 4 6 6 Child (6-12)儿童 (6-12)
5 5 21 21 Young Adult (20-29)青年 (20-29)

Here is my function in my module:这是我的模块中的功能:

def get_table(age_tuple: list) -> str:
    """Accepts a list of tuples (age, index) where the index refers to the age
    group in the get_groups() function."""
    group_id = get_groups()
    for ndx in range(len(age_tuple)):
        age = age_tuple[ndx][0]
        index = age_tuple[ndx][1]
        group = group_id[index]
        table_rows = str(ndx + 1).ljust(5) + str(age).ljust(5) + group
        print(table_rows)

I can get it to print each of the rows in my script using:我可以使用以下命令打印脚本中的每一行:

import age_analyze

ages = [20, 33, 99, 6, 21]
#age_list = age_analyze.get_age_tuples(ages)

my_list = age_analyze.get_age_tuples(ages)
table = age_analyze.get_table(my_list)

print(table)

By I feel like there is redundancy in calling print in the scrip while it has been called in the function within the module.我觉得在脚本中调用打印时存在冗余,而在模块内的函数中调用它。 Anyway, I'm sure there are easy ways of doing this but I cannot seem to get my head wrapped around this idea.无论如何,我确信有一些简单的方法可以做到这一点,但我似乎无法理解这个想法。 Help would be appreciated!帮助将不胜感激!

I believe I sorted this out on my own and it works.我相信我自己解决了这个问题并且它有效。 I simply forgot to start an empty list and use an accumulator for each row.我只是忘记启动一个空列表并为每一行使用一个累加器。 Here is the new function code:这是新的函数代码:

def get_table(age_tuple: list) -> str:
    group_id = get_groups()
    table_rows = "#".ljust(5) + "Age".ljust(5) + "Group" +"\n"
    for ndx in range(len(age_tuple)):
        age = age_tuple[ndx][0]
        index = age_tuple[ndx][1]
        group = group_id[index]
        table_rows += str(ndx + 1).ljust(5) + str(age).ljust(5) + group +"\n"
    return table_rows

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 pivot 一个表中获取 Python 中每一行的百分比? - How can I pivot a table and get the percentage of each row in Python? 如何使用Python创建一个函数,该函数将在数据表中的每一行的字典中返回一个值? - How do I create a function that will return a value in a dictionary for each row within a data sheet using Python? 我怎样才能得到一个递归的python函数来返回一个值? - How can I get a recursive python function to return a value? Python:如何返回相同的数组,其中每行删除ith元素? - Python: How can I return the same array, where the ith element is removed in each row? Python函数为每一行返回相同的值 - Python function return the same value for each row 如何将表存储在变量中,每行作为元素和分隔符,以便在Python中使用BeautifulSoup来区分列? - How can I store a table in a variable with each row as an element and a delimiter to distinguish columns using BeautifulSoup in Python? 如何在 Python 3 中获取一个函数以返回我可以在另一个函数中使用的值? - How can I get a function in Python 3 to return a value that I can use in another function? 如何重复一个函数,但每次重复返回不同的值? (蟒蛇) - How do I repeat a function but get it to return different values on each repetition? (Python) 如何在Python中获取每一行? - How to get each row in Python? 如何将 function 应用于 pandas dataframe 中的每一行? - How can I apply a function to each row in a pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM