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
我要在适当的行中打印标记和主题。
答案 0 :(得分:1)
尝试一下:
>>> for key,value in Student_Name.items():
... print("{0:20}{1:5d}".format(key,value))
从python 3.6开始,您也可以使用它。 (由Jon Clements提供)
for key,value in Student_Name.items():
print(f'{key:20}{value}')
有关其他参考,请访问this link.
答案 1 :(得分:1)
您有很多选择:
使用原始代码,您只需将下一项标签:
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
我唯一的问题是为什么要这样做,并且打印一些文件并使用定界符并稍后再担心会更好。无论哪种方式,您都应该能够完成上述操作
同样合适的是这里的第一个答案
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
始终将结果与最长的主题名称相距适当的距离。
答案 2 :(得分:-1)
与其他答案一样,为了避免第一列的硬编码宽度,您可以提前计算最大密钥长度。
简单的例子:
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