dict={'algorithm':'user1','datastr':'user2','hashing':'user3'}
def forums(dict):
header = ("SubForums","Created By")
dict_list=dict.items()
dict_list.insert(0,header)
i=0
col_width = max(len(word) for row in dict_list for word in row) + 2 # padding
for row in dict_list:
print str(i+1)+"."+"".join(word.ljust(col_width) for word in row)我想这样打印它:
SubForums Created By
1.Algorithm User1
2.Hash user2
3.datastr user3它当前的打印方式如下:
1.SubForums Created By
2.Algorithm User1
3.Hash user2
4.datastr user3有没有人可以纠正我的代码并帮助我。提前谢谢你。
发布于 2013-06-18 01:36:24
def forums(d):
dict_list = [('Subforums', 'Created by')]
for i, (k, v) in enumerate(d.items(), 1):
k = '{0}. {1}'.format(i, k)
dict_list.append((k, v))
col_width = max(len(word) for row in dict_list for word in row) + 2 # padding
for row in dict_list:
print ''.join(word.ljust(col_width) for word in row)(注意我是如何将dict更改为d的:将built-in function's name用作变量不是一个好主意!)
https://stackoverflow.com/questions/17153061
复制相似问题