Python 另一个字典,另一种格式输出
我正在处理Python嵌套字典。我有一本字典Python 另一个字典,另一种格式输出,python,loops,dictionary,output,Python,Loops,Dictionary,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} } 我需要一个循环来返回
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}
}
我需要一个循环来返回字典,它将嵌套字典分为两列。很明显,我要用的是准确的间距。只有嵌套字典上返回的值,因为我有一个头来显示每个值的含义。
我想要的输出是这样的
名称4…….5…….2…….7名称3…….5…….2…….7名称2…….5…….2…….7名称1…….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
按什么排序?我想你的意思是你只想在多个列中显示数据?@justderb只要嵌套的数据保持在一起,我不在乎哪个人和他们的数据在哪里或在哪个列中。有没有办法截断名称?我一直在玩名称键的大小,我不介意将其限制为15个字符。