[英]Why can't I use append() method in the way like that […].append(…)?
我想了解為什么我不能將 append 一個字符串添加到字符串列表中:
return ['*' * cells_per_row for j in range(self.cells // cells_per_row)].append('*' * (self.cells % cells_per_row))
但是,如果我像“通常”那樣做:
tmp = ['*' * cells_per_row for j in range(self.cells // cells_per_row)]
print(tmp)
tmp.append('*' * (self.cells % cells_per_row))
print(tmp)
tmp = '\n'.join(tmp)
print(tmp)
一切都好。
list.append
在 python 中返回None
。 你可以做的是以下。
return ['*' * cells_per_row for j in range(self.cells // cells_per_row)] + ['*' * (self.cells % cells_per_row)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.