Student_Name = {"Mathematics": 90,
"Computer Science": 100,
"Chemistry": 90,
"Physics": 97,
"English": 95}
for key,value in Student_Name.items():
print(key,value)
我想打印,如:
Mathematics 90
Computer Science 100
Chemistry 90
等等,但它是印刷像这样
Mathematics 90
Computer Science 100
Chemistry 90
Physics 97
English 95
我想打印商标和主题的合适的线路。
你有多种选择:
以你原来的代码,你可以简单地标签的下一个项目一起:
for key, value in Student_Name.items():
print(key,'\t',value)
虽然,因为它是一个标签,除非所有的按键都是相似的长度也不会看你预期这不会是完美的。
输出:
Mathematics 90
Computer Science 100
Chemistry 90
Physics 97
English 95
一个更好的解决办法是:
for key, value in Student_Name.items():
print(f'{key:20}{value}')
输出:
Mathematics 90
Computer Science 100
Chemistry 90
Physics 97
English 95
Python的3.6要求
我给你的唯一的问题是为什么,你要做到这一点,那将是更好的打印一些文件,并使用分隔符,并担心以后呈现。无论哪种方式,你应该能够与上面做
同样适合将在这里是第1个答案
for key,value in Student_Name.items():
... print("{0:20}{1:5d}".format(key,value))
这出把与F”,但是,两者都有问题,如果主题key
比别人更长的出现将需要修改。更改键{key:20}
或{0:20}
更大数量将有所帮助,但也许你可以指望检查使用填充最长为这里的值加5的钥匙的长度。
例如,你可以这样做(在用于说明目的额外添加的关键:
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)
输出:
Mathematics 90
Computer Science 100
Chemistry 90
Physics 97
English 95
REALLY LONG SUBJECT ABOUT POLITICS 10
总是使结果正确的距离最长的主题名称。
尝试这个:
>>> for key,value in Student_Name.items():
... print("{0:20}{1:5d}".format(key,value))
从蟒蛇3.6开始,你也可以用这个。 (乔恩克莱门特提供)
for key,value in Student_Name.items():
print(f'{key:20}{value}')
有关其他参考资料,请访问this link.
为了避免第一列的宽度的硬编码在其他的答案,可以预先计算出最大密钥长度。
简单的例子:
grades = {"Mathematics": 90,
"Computer Science": 100,
"Chemistry": 90,
"Physics": 97,
"English": 95}
max_key_len = max(map(len, grades.keys()))
format_string = '{{key:{}}} {{value}}'.format(max_key_len)
for key, value in grades.items():
print(format_string.format(key=key, value=value))
将打印:
Mathematics 90
Computer Science 100
Chemistry 90
Physics 97
English 95
我们可以通过一个函数进行包装,并增加了分离参数进一步提高了代码:
from typing import Dict, Iterator
def to_aligned_records(dict_: Dict,
*,
sep: str = ' ') -> Iterator[str]:
"""Yields key-value pairs as strings that will be aligned when printed"""
max_key_len = max(map(len, dict_.keys()))
format_string = '{{key:{max_len}}}{sep}{{value}}'.format(max_len=max_key_len, sep=sep)
for key, value in dict_.items():
yield format_string.format(key=key, value=value)
并使用它像这样:
>>> print(*to_aligned_records(grades), sep='\n')
Mathematics 90
Computer Science 100
Chemistry 90
Physics 97
English 95
>>> print(*to_aligned_records(grades, sep=' '*4), sep='\n')
Mathematics 90
Computer Science 100
Chemistry 90
Physics 97
English 95
>>> print(*to_aligned_records(grades, sep='\t'), sep='\n')
Mathematics 90
Computer Science 100
Chemistry 90
Physics 97
English 95