Python programming Quick Start Chapter 6 practical project reference code, python Quick Start

Source: Internet
Author: User

Python programming Quick Start Chapter 6 practical project reference code, python Quick Start

The Code is as follows:

A function is used to display the list in a well-organized table. Each column is right aligned.

tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]

'''

Apples Alice dogs
Oranges Bob cats
Cherries Carol moose
Banana David goose

'''

# The right alignment of each output column should not be the last alignment of the string,

# But not on the book. It bothered me for one night.

def printTable(tableData):
colWidths = [0] * len(tableData)
col = []
for i in range(0, len(tableData[0])):
for j in range(0, len(colWidths)):
col.append(len(tableData[j][i]))
max_len = max(col)

for i in range(0, len(tableData[0])):
for j in range(0, len(colWidths)):
print(tableData[j][i].rjust(max_len),end='')
print()

if __name__ == '__main__':
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
printTable(tableData)


----------------------------------------------------------------
----------------------------------------------------------------

Apples Alice dogs
Oranges Bob cats
Cherries Carol moose
Banana David goose

This is the only way to align the right of each column. If you have any idea how to solve this problem, please leave a message.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.