将 Python 2D 矩阵/列表变成表格 [英] Turn the Python 2D matrix/list into a table

查看:26
本文介绍了将 Python 2D 矩阵/列表变成表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能把这个:

students = [("Abe", 200), ("Lindsay", 180), ("Rachel", 215)]

进入这个:

安倍200林赛180第 215 章

这应该适用于任何大小的列表.

解决方案

使用 string格式:

<预><代码>>>>学生 = [("Abe", 200), ("Lindsay", 180), ("Rachel", 215)]>>>对于 a, b 学生:... 打印 '{:<7s} {}'.format(a, b)...安倍200林赛180第 215 章

How can I turn this:

students = [("Abe", 200), ("Lindsay", 180), ("Rachel" , 215)]

into this:

Abe     200
Lindsay 180
Rachel  215

EDIT: This should be able to work for any size list.

解决方案

Use string formatting:

>>> students = [("Abe", 200), ("Lindsay", 180), ("Rachel" , 215)]
>>> for a, b in students:
...     print '{:<7s} {}'.format(a, b)
...
Abe     200
Lindsay 180
Rachel  215

这篇关于将 Python 2D 矩阵/列表变成表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆