将列表拆分成行
Split the list into lines
我有一个full_list如下:
full_list = [[[-180, 90], [-180, 80], [-175, 80], [-175, 90]], [[-180, 80], [-180, 70], [-175, 70], [-175, 80]], [[-180, 70], [-180, 60], [-175, 60], [-175, 70]], [[-180, 60], [-180, 50], [-175, 50], [-175, 60]]]
如何生成如下所示的名为 ans 的列表?
ans = [[[-180, 90], [-180, 80], [-175, 80], [-175, 90]],
[[-180, 80], [-180, 70], [-175, 70], [-175, 80]],
[[-180, 70], [-180, 60], [-175, 60], [-175, 70]],
[[-180, 60], [-180, 50], [-175, 50], [-175, 60]]]
两个列表相同,唯一不同的是后面的列表被拆分成行。
你是说漂亮的印刷吗?你的列表已经是那样了。
>>> full_list = [[[-180, 90], [-180, 80], [-175, 80], [-175, 90]], [[-180, 80], [-180, 70], [-175, 70], [-175, 80]], [[-180, 70], [-180, 60], [-175, 60], [-175, 70]], [[-180, 60], [-180, 50], [-175, 50], [-175, 60]]]
>>> from pprint import pprint
>>> pprint(full_list)
[[[-180, 90], [-180, 80], [-175, 80], [-175, 90]],
[[-180, 80], [-180, 70], [-175, 70], [-175, 80]],
[[-180, 70], [-180, 60], [-175, 60], [-175, 70]],
[[-180, 60], [-180, 50], [-175, 50], [-175, 60]]]
正如另一个答案中提到的,pprint
是自然的方式。
但根据您的具体需求,您也可以这样做:
print(*full_list, sep='\n')
# Output:
[[-180, 90], [-180, 80], [-175, 80], [-175, 90]]
[[-180, 80], [-180, 70], [-175, 70], [-175, 80]]
[[-180, 70], [-180, 60], [-175, 60], [-175, 70]]
[[-180, 60], [-180, 50], [-175, 50], [-175, 60]]
要自定义一点,您可以使用 range
:
def my_custom_printer(full_list):
for i in range(0, len(full_list), 2):
try:
print("{}, {}".format(full_list[i], full_list[i+1]))
except IndexError:
# len(full_list) is odd, so just print the last index
print(full_list[i])
my_custom_printer(full_list)
# Output:
[[-180, 90], [-180, 80], [-175, 80], [-175, 90]], [[-180, 80], [-180, 70], [-175, 70], [-175, 80]]
[[-180, 70], [-180, 60], [-175, 60], [-175, 70]], [[-180, 60], [-180, 50], [-175, 50], [-175, 60]]
但后来有点脏了...主要是因为实际数据和它的表示不再严格一致了
我有一个full_list如下:
full_list = [[[-180, 90], [-180, 80], [-175, 80], [-175, 90]], [[-180, 80], [-180, 70], [-175, 70], [-175, 80]], [[-180, 70], [-180, 60], [-175, 60], [-175, 70]], [[-180, 60], [-180, 50], [-175, 50], [-175, 60]]]
如何生成如下所示的名为 ans 的列表?
ans = [[[-180, 90], [-180, 80], [-175, 80], [-175, 90]],
[[-180, 80], [-180, 70], [-175, 70], [-175, 80]],
[[-180, 70], [-180, 60], [-175, 60], [-175, 70]],
[[-180, 60], [-180, 50], [-175, 50], [-175, 60]]]
两个列表相同,唯一不同的是后面的列表被拆分成行。
你是说漂亮的印刷吗?你的列表已经是那样了。
>>> full_list = [[[-180, 90], [-180, 80], [-175, 80], [-175, 90]], [[-180, 80], [-180, 70], [-175, 70], [-175, 80]], [[-180, 70], [-180, 60], [-175, 60], [-175, 70]], [[-180, 60], [-180, 50], [-175, 50], [-175, 60]]]
>>> from pprint import pprint
>>> pprint(full_list)
[[[-180, 90], [-180, 80], [-175, 80], [-175, 90]],
[[-180, 80], [-180, 70], [-175, 70], [-175, 80]],
[[-180, 70], [-180, 60], [-175, 60], [-175, 70]],
[[-180, 60], [-180, 50], [-175, 50], [-175, 60]]]
正如另一个答案中提到的,pprint
是自然的方式。
但根据您的具体需求,您也可以这样做:
print(*full_list, sep='\n')
# Output:
[[-180, 90], [-180, 80], [-175, 80], [-175, 90]]
[[-180, 80], [-180, 70], [-175, 70], [-175, 80]]
[[-180, 70], [-180, 60], [-175, 60], [-175, 70]]
[[-180, 60], [-180, 50], [-175, 50], [-175, 60]]
要自定义一点,您可以使用 range
:
def my_custom_printer(full_list):
for i in range(0, len(full_list), 2):
try:
print("{}, {}".format(full_list[i], full_list[i+1]))
except IndexError:
# len(full_list) is odd, so just print the last index
print(full_list[i])
my_custom_printer(full_list)
# Output:
[[-180, 90], [-180, 80], [-175, 80], [-175, 90]], [[-180, 80], [-180, 70], [-175, 70], [-175, 80]]
[[-180, 70], [-180, 60], [-175, 60], [-175, 70]], [[-180, 60], [-180, 50], [-175, 50], [-175, 60]]
但后来有点脏了...主要是因为实际数据和它的表示不再严格一致了