Skip to main content
 首页 » 编程设计

python之将单个列表放在新行上

2025年05月04日12lhb25

来自死脑筋的该死的愚蠢问题...

我有一个列表:

[1,2,3,4,5,6,7,8,9] 

我将其分成 3 个列表:

splits = [1,2,3],[4,5,6],[7,8,9] 

我现在想在单独的行上打印

print splits 

给予

[1,2,3] 
[4,5,6] 
[7,8,9] 

有人可以 1) 把我的头打翻 2) 提醒我怎么做吗?

请您参考如下方法:

如果

s = [[1,2,3],[4,5,6],[7,8,9]] # list of lists 

或

s = [1,2,3],[4,5,6],[7,8,9]   # a tuple of lists 

然后

for i in s: 
   print(i) 

将导致:

[1, 2, 3] 
[4, 5, 6] 
[7, 8, 9] 

在 的指导下 Zen of Python :简单胜于复杂。