Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

python - How to print key-value pairs of a dict as an aligned table?

Student_Name = {"Mathematics": 90, 
                "Computer Science": 100, 
                "Chemistry": 90, 
                "Physics": 97, 
                "English": 95}
for key,value in Student_Name.items():
    print(key,value)

I want to print like:

Mathematics        90
Computer Science   100
Chemistry          90 

and so on but it is printing like this

Mathematics 90
Computer Science 100
Chemistry 90
Physics 97
English 95

I want to print marks and subjects in proper line.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You've got a number of options:

Taking your original code you could simply tab the next item along:

for key, value in Student_Name.items():
    print(key,'',value)

Although this wouldn't be perfect as it's a tab, and unless all the keys are similar length it wouldn't look as you intended.

Output:

Mathematics      90
Computer Science         100
Chemistry        90
Physics          97
English          95

A better solution could would be:

for key, value in Student_Name.items():
    print(f'{key:20}{value}')

output:

Mathematics         90
Computer Science    100
Chemistry           90
Physics             97
English             95

Python 3.6 required

My only question to you is why, you want to do this and would it be better to print some file and use delimiter and worry about presentation later. Either way you should be able to do with the above

Equally suitable would be the 1st answer here

for key,value in Student_Name.items():
...     print("{0:20}{1:5d}".format(key,value))

Which out puts the same as f' but, both have the problem that if the subject key is much longer than the others the appearance will need to be amended. Changing key {key:20} or {0:20} to a greater number will help, but maybe you could count check the length of your keys using the longest as the value here plus 5 for padding.

for example you could do this (added in an extra key for illustration purposes:

 Student_Name = {"Mathematics": 90, "Computer Science": 100, "Chemistry": 90, "Physics": 97, "English": 95, "REALLY LONG SUBJECT ABOUT POLITICS": 10}
 # Get the longest subject name
 length = max(len(x) for x in Student_Name)
 # decide on padding
 padding = 5

 #use these two value to working out the exact space required
 space = length + padding

 #format and print the statement
 for key, value in Student_Name.items():
 ...     subject = "{0:{space}}".format(key, space=space)
 ...     result = "{0:5d}".format(value)
 ...     print(subject + result)

Output :

Mathematics                           90
Computer Science                     100
Chemistry                             90
Physics                               97
English                               95
REALLY LONG SUBJECT ABOUT POLITICS    10

Would always make the result the right distance away from the longest subject name.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...