Python print() 打印两个 list ,实现中间换行
有两个 list ,如果直接输出 print(list1, list2) 则会打印出一团,如下:
[0.867, 0.932, 0.95, 0.942, 0.941, 0.939, 0.939, 0.95, 0.941, 0.916, 0.879, 0.869, 0.869, 0.865, 0.851, 0.838, 0.845, 0.823, 0.829, 0.835, 0.841, 0.829, 0.842, 0.849, 0.85, 0.833, 0.868, 0.898, 0.911, 0.908, 0.912, 0.895, 0.899, 0.9, 0.927, 0.909, 0.909, 0.899, 0.919, 0.921, 0.926] [0.884, 0.939, 0.928, 0.924, 0.922, 0.921, 0.923, 0.933, 0.94, 0.913, 0.865, 0.845, 0.85, 0.859, 0.823, 0.793, 0.79, 0.78, 0.804, 0.803, 0.823, 0.816, 0.828, 0.824, 0.818, 0.81, 0.847, 0.878, 0.878, 0.878, 0.888, 0.887, 0.881, 0.881, 0.893, 0.88, 0.882, 0.879, 0.889, 0.885, 0.897]
看着很乱,所以想一个 list 一行,则代码改为:
print('list1:',list1,'\n','list2:',list2)
输入结果如下
list1: [0.867, 0.932, 0.95, 0.942, 0.941, 0.939, 0.939, 0.95, 0.941, 0.916, 0.879, 0.869, 0.869, 0.865, 0.851, 0.838, 0.845, 0.823, 0.829, 0.835, 0.841, 0.829, 0.842, 0.849, 0.85, 0.833, 0.868, 0.898, 0.911, 0.908, 0.912, 0.895, 0.899, 0.9, 0.927, 0.909, 0.909, 0.899, 0.919, 0.921, 0.926] list2: [0.884, 0.939, 0.928, 0.924, 0.922, 0.921, 0.923, 0.933, 0.94, 0.913, 0.865, 0.845, 0.85, 0.859, 0.823, 0.793, 0.79, 0.78, 0.804, 0.803, 0.823, 0.816, 0.828, 0.824, 0.818, 0.81, 0.847, 0.878, 0.878, 0.878, 0.888, 0.887, 0.881, 0.881, 0.893, 0.88, 0.882, 0.879, 0.889, 0.885, 0.897]
可以看出 list2 前面有个小空格,原因为:
print里面一个逗号就会有一个空格,\n后面有个逗号所以换行之后就有空格了。
所以将代码改为:
print('list1:',list1,'\n''list2:',list2)
输出结果如下:
list1: [0.867, 0.932, 0.95, 0.942, 0.941, 0.939, 0.939, 0.95, 0.941, 0.916, 0.879, 0.869, 0.869, 0.865, 0.851, 0.838, 0.845, 0.823, 0.829, 0.835, 0.841, 0.829, 0.842, 0.849, 0.85, 0.833, 0.868, 0.898, 0.911, 0.908, 0.912, 0.895, 0.899, 0.9, 0.927, 0.909, 0.909, 0.899, 0.919, 0.921, 0.926] list2: [0.884, 0.939, 0.928, 0.924, 0.922, 0.921, 0.923, 0.933, 0.94, 0.913, 0.865, 0.845, 0.85, 0.859, 0.823, 0.793, 0.79, 0.78, 0.804, 0.803, 0.823, 0.816, 0.828, 0.824, 0.818, 0.81, 0.847, 0.878, 0.878, 0.878, 0.888, 0.887, 0.881, 0.881, 0.893, 0.88, 0.882, 0.879, 0.889, 0.885, 0.897]
以上,问题解决~