假设您已经拥有最大列数并且已经将它们分成列表(我将假设您将其放入自己的列表中),您应该能够只使用 list.insert(-1,item)添加空列:
def columnize(mylists, maxcolumns):
for i in mylists:
while len(i) < maxcolumns:
i.insert(-1,None)
mylists = [["author1","author2","author3","this is the title of the article"],
["author1","author2","this is the title of the article"],
["author1","author2","author3","author4","this is the title of the article"]]
columnize(mylists,5)
print mylists
[['author1', 'author2', 'author3', None, 'this is the title of the article'], ['author1', 'author2', None, None, 'this is the title of the article'], ['author1', 'author2', 'author3', 'author4', 'this is the title of the article']]
使用列表推导不会破坏原始列表的替代版本:
def columnize(mylists, maxcolumns):
return [j[:-1]+([None]*(maxcolumns-len(j)))+j[-1:] for j in mylists]
print columnize(mylists,5)
[['author1', 'author2', 'author3', None, 'this is the title of the article'], ['author1', 'author2', None, None, 'this is the title of the article'], ['author1', 'author2', 'author3', 'author4', 'this is the title of the article']]