이미 최대 개수의 열을 가지고 있으며 이미 목록으로 분리되어 있다고 가정하면 (목록을 자신의 목록에 넣었 음을 가정합니다.) list.insert (- 지능형리스트 사용하여 원래의 목록을 파괴하지 않는
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']]
대체 버전 : 1 항목) 빈 열을 추가하는
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']]
기사 제목이 항상 목록의 마지막 항목이라고 가정하는 것이 안전합니까? 또한 어떤 접근 방식을 시도해 보셨습니까? –
가변 개수의 열과 함께 작업하지만이 작업은 수행되지 않습니다. 정해진 수의 열이 있어야합니다. 목록을 작성하고 추가하려고했지만 목록에 빈 항목을 추가하는 데 어려움이 있습니다. – aeupinhere
이것은 내가 서있는 곳입니다 ... http : //pastebin.com/A2CT97s9 – aeupinhere