Python 如何在每行打印不同的标题

Python 如何在每行打印不同的标题,python,python-3.x,Python,Python 3.x,目前我有一些数据: EXAMPLE_DATA = [ ['time', 'age', 'height', 'width', 'ethnicity', 'religion'], ['18:42:11', '61', '153.9615', '0.8', 'Mixed', 'None'], ['18:35:00', '34', '116.4253333', '10.17', 'Mixed', 'None']] 我有一个名为“example_func”的函数,它调用examp

目前我有一些数据:

EXAMPLE_DATA = [
    ['time', 'age', 'height', 'width', 'ethnicity', 'religion'],
    ['18:42:11', '61', '153.9615', '0.8', 'Mixed', 'None'],
    ['18:35:00', '34', '116.4253333', '10.17', 'Mixed', 'None']]
我有一个名为“example_func”的函数,它调用example_数据[1],例如:数据的第二行

然后我使用代码:

def display_data( example_func ):

    for row in example_func:
        print(row)
这将提供以下输出:

18:42:11
61
153.9615
0.8
Mixed
None
我希望以下输出是:

Time: 18:42:11
Age: 61
Height: 153.9615
Ethnicity: Mixed
但是,我想在代码中设置标题,不想使用示例_数据中的标题

正如您所注意到的,我也不想在最终输出中显示“宽度”或“宗教”


如果您需要更多信息,请告诉我。

使用熊猫解决您的问题。Pandas将按照您要求的格式自动处理数据。您可以根据需要修改输出。

使用熊猫解决您的问题。Pandas将按照您要求的格式自动处理数据。您可以根据自己的意愿修改输出。

不确定我是否完全理解您的所有问题,但这里有一个猜测:

from collections import namedtuple

EXAMPLE_DATA = [
    ['time', 'age', 'height', 'width', 'ethnicity', 'religion'],
    ['18:42:11', '61', '153.9615', '0.8', 'Mixed', 'None'],
    ['18:35:00', '34', '116.4253333', '10.17', 'Mixed', 'None']]

def display_data(example_func):
    Record = namedtuple('Record', example_func[0])

    for row in example_func[1:]:
        print('Time: {time}\n'
              'Age: {age}\n'
              'Height: {height}\n'
              'Ethnicity: {ethnicity}\n'.format(**Record(*row)._asdict()))

display_data(EXAMPLE_DATA)
您可以使用Python 3.6+更简洁地编写它

def display_data(example_func):
    Record = namedtuple('Record', example_func[0])

    for rec in (Record(*row) for row in example_func[1:]):
        print(f'Time: {rec.time}\n'
              f'Age: {rec.age}\n'
              f'Height: {rec.height}\n'
              f'Ethnicity: {rec.ethnicity}\n')

我不确定我是否理解你的所有问题,但这里有一个猜测:

from collections import namedtuple

EXAMPLE_DATA = [
    ['time', 'age', 'height', 'width', 'ethnicity', 'religion'],
    ['18:42:11', '61', '153.9615', '0.8', 'Mixed', 'None'],
    ['18:35:00', '34', '116.4253333', '10.17', 'Mixed', 'None']]

def display_data(example_func):
    Record = namedtuple('Record', example_func[0])

    for row in example_func[1:]:
        print('Time: {time}\n'
              'Age: {age}\n'
              'Height: {height}\n'
              'Ethnicity: {ethnicity}\n'.format(**Record(*row)._asdict()))

display_data(EXAMPLE_DATA)
您可以使用Python 3.6+更简洁地编写它

def display_data(example_func):
    Record = namedtuple('Record', example_func[0])

    for rec in (Record(*row) for row in example_func[1:]):
        print(f'Time: {rec.time}\n'
              f'Age: {rec.age}\n'
              f'Height: {rec.height}\n'
              f'Ethnicity: {rec.ethnicity}\n')

下面是一个执行此任务的函数:

def display_data(example_func):

    headings = ['Time', 'Age', 'Height', 'Ethnicity']  # Your headings
    rows = [0, 1, 2, 4]  # The corresponding rows

    for heading, row in zip(headings, rows):  # extracts each heading and the corresponding row
        print(heading + ': ' + example_func[row])

下面是一个执行此任务的函数:

def display_data(example_func):

    headings = ['Time', 'Age', 'Height', 'Ethnicity']  # Your headings
    rows = [0, 1, 2, 4]  # The corresponding rows

    for heading, row in zip(headings, rows):  # extracts each heading and the corresponding row
        print(heading + ': ' + example_func[row])

注意:仅当您传递整个表格而不是一行时,以下操作才有效。对不起,我疏忽了。这样更优雅。如果您真的希望它只在一行上工作,那么示例代码就在最后

您可以制作一个字典,将您想要的标题映射到它们在
示例\u DATA
中的索引:

 HEADINGS = {'Time':0, 'Age':1, 'Height':2, 'Ethnicity':4}
有了它,您只需遍历这些键,并在相应的索引处显示值

def display_data( example_func, headings ):
    for row in example_func:
        for key in headings.keys():
            print(key + ": " + row[headings[key]]
但是,如果您想要绝对的效率,您可以计算一次
headers.keys()
,然后多次使用它。没关系

def display_data( example_func, headings ):
    keys = headings.keys()
    for row in example_func():
        for key in keys:
            print(key + ": " + row[headings[key]]
单行示例代码:

def display_data( example_func, headings ):
    keys = headings.keys()
    for row in [example_func()]:
        for key in keys:
            print(key + ": " + row[headings[key]]

注意:仅当您传递整个表格而不是一行时,以下操作才有效。对不起,我疏忽了。这样更优雅。如果您真的希望它只在一行上工作,那么示例代码就在最后

您可以制作一个字典,将您想要的标题映射到它们在
示例\u DATA
中的索引:

 HEADINGS = {'Time':0, 'Age':1, 'Height':2, 'Ethnicity':4}
有了它,您只需遍历这些键,并在相应的索引处显示值

def display_data( example_func, headings ):
    for row in example_func:
        for key in headings.keys():
            print(key + ": " + row[headings[key]]
但是,如果您想要绝对的效率,您可以计算一次
headers.keys()
,然后多次使用它。没关系

def display_data( example_func, headings ):
    keys = headings.keys()
    for row in example_func():
        for key in keys:
            print(key + ": " + row[headings[key]]
单行示例代码:

def display_data( example_func, headings ):
    keys = headings.keys()
    for row in [example_func()]:
        for key in keys:
            print(key + ": " + row[headings[key]]

非常感谢。除了我只想显示“时间”、“年龄”、“身高”和“种族”之外,其他一切都正常工作,我怎么能做到这一点?好的……给我一分钟。谢谢!除了我只想显示“时间”、“年龄”、“身高”和“种族”之外,其他一切都可以正常工作,我该怎么做?好的……给我一分钟。