Python style list formatting
本问题已经有最佳答案,请猛点这里访问。
我正在努力提高我的Python技能、列表和列表格式。我在网上做了一些练习,遇到了一个似乎无法解决的问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # [ ] The list `data` contains personnel information (Name, ID, email) # Write a program using Python-style formatting to print out the data as follows: ''' Name | ID | Email ________________________________________________________ Suresh Datta | 57394 | [email protected] Colette Browning | 48539 | [email protected] Skye Homsi | 58302 | [email protected] Hiroto Yamaguchi | 48502 | [email protected] Tobias Ledford | 48291 | [email protected] Jin Xu | 48293 | [email protected] Joana Dias | 23945 | [email protected] Alton Derosa | 85823 | [email protected] ''' data = [ ["Suresh Datta", 57394,"[email protected]"] , ["Colette Browning", 48539,"[email protected]"] , ["Skye Homsi", 58302,"[email protected]"] , ["Hiroto Yamaguchi", 48502,"[email protected]"] , ["Tobias Ledford", 48291,"[email protected]"] , ["Tamara Babic", 58201,"[email protected]"] , ["Jin Xu", 48293,"[email protected]"] , ["Joana Dias", 23945,"[email protected]"] , ["Alton Derosa", 85823,"[email protected]"] ] |
我已经设法解决了其他问题,但这是唯一一个我不知道该怎么做的问题。感谢您的帮助。谢谢!
使用Python 3.6 + F。:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | data = [["Suresh Datta", 57394,"[email protected]"], ["Colette Browning", 48539,"[email protected]"], ["Skye Homsi", 58302,"[email protected]"], ["Hiroto Yamaguchi", 48502,"[email protected]"], ["Tobias Ledford", 48291,"[email protected]"], ["Tamara Babic", 58201,"[email protected]"], ["Jin Xu", 48293,"[email protected]"], ["Joana Dias", 23945,"[email protected]"], ["Alton Derosa", 85823,"[email protected]"]] print(f'{"Name":^21}|{"ID":^12}|{"Email":^21}') print('_'*56) for name,id,email in data: print(f'{name:^21}|{id:^12}|{email:>21}') |
输出:
1 2 3 4 5 6 7 8 9 10 11 | Name | ID | Email ________________________________________________________ Suresh Datta | 57394 | suresh@example.com Colette Browning | 48539 | colette@example.com Skye Homsi | 58302 | skye@example.com Hiroto Yamaguchi | 48502 | hiroto@example.com Tobias Ledford | 48291 | tobias@example.com Tamara Babic | 58201 | tamara@example.com Jin Xu | 48293 | jin@example.com Joana Dias | 23945 | joana@example.com Alton Derosa | 85823 | alton@example.com |
在一个F的字符串,是
1 2 3 4 | print('{:^30} |{:^30}|{:^30}' .format("Name","ID","Email")) print('_'*90) for s in data: print('{:^30} |{:^30}|{:^30}' .format(s[0],s[1],s[2])) |
输出
1 2 3 4 5 6 7 8 9 10 11 | Name | ID | Email __________________________________________________________________________________________ Suresh Datta | 57394 | suresh@example.com Colette Browning | 48539 | colette@example.com Skye Homsi | 58302 | skye@example.com Hiroto Yamaguchi | 48502 | hiroto@example.com Tobias Ledford | 48291 | tobias@example.com Tamara Babic | 58201 | tamara@example.com Jin Xu | 48293 | jin@example.com Joana Dias | 23945 | joana@example.com Alton Derosa | 85823 | alton@example.com |