从我的 Python 控制台
>>> numbers = [1,2,3]
>>> [print(x) for x in numbers]
1
2
3
[None, None, None]
为什么最后打印三个无?
最佳答案
您应该重构循环以将参数发送到 print()
:
>>> numbers = [1,2,3]
>>> print(*(x for x in numbers), sep='\n')
请注意,您不需要显式生成器。只需解压 list
本身:
>>> numbers = [1,2,3]
>>> print(*numbers, sep='\n')
关于python - 使用列表理解打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37084246/