使用 [print(n) for n in list] 和 google colab
using [print(n) for n in list] with google colab
我想这样打印一个列表:
mylist = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9]
[print(n) for n in mylist]
之所以这样做是因为我想以垂直方式查看所有项目,例如在 for 循环中打印内容的方式。
我以前用 pycharm 经常这样做,没问题。
但是说到 google colab。
它给我打印了一些额外的东西:
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
[None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None]
如何避免或删除 google colab 中的 None 列表。
您可能不想使用列表理解来打印内容;它有效,但令人困惑。一个for
循环会更清晰:
mylist = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9]
for n in mylist:
print(n)
就 google colab 中发生的事情而言,当您编写 [print(n) for n in mylist]
时,它会创建一个包含 print()
语句的所有结果的列表,这些结果都是 None
. Google colab 然后打印最后一条语句的结果。
看来,你不想使用for循环。
您可以执行以下操作:
mylist = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9]
_ = [print(n) for n in mylist]
可以使用pprint实现漂亮的打印
from pprint import pprint
lis = list(range(1000))
pprint(lis)
我想这样打印一个列表:
mylist = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9]
[print(n) for n in mylist]
之所以这样做是因为我想以垂直方式查看所有项目,例如在 for 循环中打印内容的方式。
我以前用 pycharm 经常这样做,没问题。
但是说到 google colab。
它给我打印了一些额外的东西:
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
[None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None]
如何避免或删除 google colab 中的 None 列表。
您可能不想使用列表理解来打印内容;它有效,但令人困惑。一个for
循环会更清晰:
mylist = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9]
for n in mylist:
print(n)
就 google colab 中发生的事情而言,当您编写 [print(n) for n in mylist]
时,它会创建一个包含 print()
语句的所有结果的列表,这些结果都是 None
. Google colab 然后打印最后一条语句的结果。
看来,你不想使用for循环。
您可以执行以下操作:
mylist = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9]
_ = [print(n) for n in mylist]
可以使用pprint实现漂亮的打印
from pprint import pprint
lis = list(range(1000))
pprint(lis)