python - 从列表中以表格格式排列数据
问题描述
我有一个如下列表,并希望将其显示在 5 列中,一列一列
lsit = ['Alexander City', 'Andalusia', 'Anniston', 'Athens', 'Atmore',
'Auburn', 'Bessemer', 'Birmingham', 'Chickasaw', 'Clanton',
'Cullman', 'Decatur', 'Demopolis', 'Dothan', 'Enterprise',
'Eufaula', 'Florence', 'Fort Payne', 'Gadsden', 'Greenville',
'Guntersville', 'Huntsville']
尝试过,print("\t".join(lsit))
但数据未正确填充
>>> print("\t".join(lsit))
Alexander City Andalusia Anniston Athens Atmore Auburn Bessemer Birmingham Chickasaw Clanton Cullman DecaturDemopolis Dothan Enterprise Eufaula Florence Fort Payne Gadsden Greenville Guntersville Huntsville
有人可以指导如何在 python 中实现这一点吗?
解决方案
那么你所缺少的就是以五个一组的方式打印它们:
for i in range(0, len(lsit), 5):
print("\t".join(lsit[i: i+5]))
这使:
Alexander City Andalusia Anniston Athens Atmore
Auburn Bessemer Birmingham Chickasaw Clanton
Cullman Decatur Demopolis Dothan Enterprise
Eufaula Florence Fort Payne Gadsden Greenville
Guntersville Huntsville
推荐阅读
- c# - 输出文件上的 System.IO.FileNotFoundException
- asp.net-mvc-5 - 在启用sustainsys OWIN 中间件时发出注销非SAML 身份验证用户的问题
- mypy - mypy:如何忽略 mixins 中的“缺少属性”错误
- javascript - 如何在基于 WordPress 的网站中发布自定义表单?
- c# - Unity3D C#:Spawner 不断实例化超过分配的值
- azure - 将 Azure AD 来宾用户的来源从“Microsoft 帐户”更改为“外部 AAD”(Azure B2B)
- html - IE11 表格单元格高度
- bash - 无法在 curl 中使用变量
- c++ - char* 数组在 C++ 中的基地址
- kdb - 如何将格式为“dd/MM/yyyy”的字符串解析为q kdb中的日期类型?