另一个字典,另一种格式输出
Another Dictionary, Another Format Output
我正在处理 Python 嵌套字典。我有字典;
contacts = {
'name4': {'x': 5, 'y': 2, 'z': 7},
'Name2': {'x': 5, 'y': 2, 'z': 5},
'name3': {'x': 5, 'y': 2, 'z': 7},
'Name1': {'x': 5, 'y': 2, 'z': 7}
}
我需要一个循环,returns 将 nest 字典分为 2 列的字典。显然我必须使用的确切间距。只有嵌套字典上返回的值,因为我有一个 header 来显示每个值的含义。
我想要的输出是这样的;
Name4........5........2........7 Name3........5........2........7
Name2........5........2........7 Name1........5........2........7
尝试这种索引检查技术,您可以在其中添加 '\t'
或 '\n'
string = ''
for i in range(len(contacts.items())):
string += contacts.items()[i][0] + ': '
string += ' '.join([str(j) for j in contacts.items()[i][1].values()])
if i % 2 == 0:
string += '\t'
if i % 2 != 0:
string += '\n'
print string # prints in two columns
我正在处理 Python 嵌套字典。我有字典;
contacts = {
'name4': {'x': 5, 'y': 2, 'z': 7},
'Name2': {'x': 5, 'y': 2, 'z': 5},
'name3': {'x': 5, 'y': 2, 'z': 7},
'Name1': {'x': 5, 'y': 2, 'z': 7}
}
我需要一个循环,returns 将 nest 字典分为 2 列的字典。显然我必须使用的确切间距。只有嵌套字典上返回的值,因为我有一个 header 来显示每个值的含义。 我想要的输出是这样的;
Name4........5........2........7 Name3........5........2........7
Name2........5........2........7 Name1........5........2........7
尝试这种索引检查技术,您可以在其中添加 '\t'
或 '\n'
string = ''
for i in range(len(contacts.items())):
string += contacts.items()[i][0] + ': '
string += ' '.join([str(j) for j in contacts.items()[i][1].values()])
if i % 2 == 0:
string += '\t'
if i % 2 != 0:
string += '\n'
print string # prints in two columns