
本文旨在介绍如何使用 Python 编程语言,在不依赖任何外部模块的前提下,将列表和嵌套列表的数据以表格形式进行格式化输出。文章将详细讲解如何利用 zip() 函数以及字符串格式化技巧,实现美观且易于阅读的表格数据呈现,并提供完整的代码示例和解释。
在数据处理和展示中,将数据以表格形式呈现是一种常见的需求。Python 提供了多种方式来实现这一目标,本教程将重点介绍如何使用内置函数和字符串格式化来实现。这种方法不需要导入任何额外的库,因此非常适合在资源受限或者需要最小化依赖的环境中使用。
当需要将两个或多个列表中的元素一一对应地组合在一起时,zip() 函数是一个非常有用的工具。例如,如果有一个包含国家名称的列表和一个包含对应奖牌数量的嵌套列表,可以使用 zip() 函数将它们组合成一个可迭代的对象,方便后续处理。
countries = [
"Canada",
"Italy",
"Germany",
"Japan",
"Kazakhstan",
"China",
"South Korea",
"United States"
]
counts = [
[ 0, 3, 0 ],
[ 0, 0, 1 ],
[ 0, 0, 1 ],
[ 1, 0, 0 ],
[ 0, 0, 1 ],
[ 3, 1, 1 ],
[ 0, 1, 0 ],
[ 1, 0, 1 ]
]
for country_name, medal_counts in zip(countries, counts):
gold, silver, bronze = medal_counts
total = sum(medal_counts)
print(country_name, gold, silver, bronze, total)在上述代码中,zip(countries, counts) 将 countries 列表和 counts 列表中的元素一一对应地组合在一起。在循环中,每次迭代都会得到一个国家名称和一个包含奖牌数量的列表。然后,将奖牌数量列表解包为金牌、银牌和铜牌的数量,并计算总奖牌数。最后,将这些数据打印出来。
立即学习“Python免费学习笔记(深入)”;
当然,不使用 zip() 函数,也可以通过索引来访问列表中的元素,实现相同的功能:
for index in range(len(countries)):
country_name = countries[index]
medal_counts = counts[index]
gold, silver, bronze = medal_counts
total = sum(medal_counts)
print(country_name, gold, silver, bronze, total)虽然这种方法也能达到相同的目的,但使用 zip() 函数可以使代码更简洁、更易读。
仅仅将数据打印出来是不够的,为了使输出更美观,可以使用字符串格式化来控制数据的对齐方式和间距。Python 提供了多种字符串格式化的方法,这里推荐使用 format() 方法,因为它更灵活、更易于使用。
template = "{country_name:>15} {gold:>8} {silver:>8} {bronze:>8} {total:>8} "
print(template.format(country_name="", gold="Gold", silver="Silver", bronze="Bronze", total="Total"))
for country_name, medal_counts in zip(countries, counts):
gold, silver, bronze = medal_counts
total = sum(medal_counts)
print(template.format(country_name=country_name, gold=gold, silver=silver, bronze=bronze, total=total))在上述代码中,template 变量定义了一个格式化字符串,其中包含了五个占位符:country_name、gold、silver、bronze 和 total。每个占位符后面的 > 符号表示右对齐,数字表示占用的字符宽度。例如,{country_name:>15} 表示 country_name 字段右对齐,占用 15 个字符的宽度。
通过调用 format() 方法,可以将实际的数据填充到占位符中,生成格式化后的字符串。首先,打印表头,然后循环遍历每个国家和对应的奖牌数量,打印表格的每一行。
将以上两个部分结合起来,可以得到一个完整的代码示例:
# Create a list of country names.
countries = [
"Canada",
"Italy",
"Germany",
"Japan",
"Kazakhstan",
"China",
"South Korea",
"United States"
]
# Create a table of medal counts.
counts = [
[ 0, 3, 0 ],
[ 0, 0, 1 ],
[ 0, 0, 1 ],
[ 1, 0, 0 ],
[ 0, 0, 1 ],
[ 3, 1, 1 ],
[ 0, 1, 0 ],
[ 1, 0, 1 ]
]
## -----------------
## Now we just need to make it look nice
## -----------------
template = "{country_name:>15} {gold:>8} {silver:>8} {bronze:>8} {total:>8} "
## -----------------
print(template.format(country_name="", gold="Gold", silver="Silver", bronze="Bronze", total="Total"))
for country_name, medal_counts in zip(countries, counts):
gold, silver, bronze = medal_counts
total = sum(medal_counts)
print(template.format(country_name=country_name, gold=gold, silver=silver, bronze=bronze, total=total))这段代码的输出结果如下:
Gold Silver Bronze Total
Canada 0 3 0 3
Italy 0 0 1 1
Germany 0 0 1 1
Japan 1 0 0 1
Kazakhstan 0 0 1 1
China 3 1 1 5
South Korea 0 1 0 1
United States 1 0 1 2本文介绍了如何使用 Python 内置函数 zip() 和字符串格式化技巧,将列表和嵌套列表的数据以表格形式进行格式化输出。这种方法简单易懂,不需要导入任何额外的库,非常适合在各种场景中使用。掌握这些技巧可以帮助您更好地处理和展示数据,提高工作效率。
以上就是使用 Python 格式化输出列表和嵌套列表,创建表格形式的数据展示的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号