Python 列中显示的嵌套列表
我正在尝试将嵌套列表显示为列。因此,我正在处理的数据是:Python 列中显示的嵌套列表,python,python-3.x,Python,Python 3.x,我正在尝试将嵌套列表显示为列。因此,我正在处理的数据是: tableData = [['apples', 'oranges', 'cherries', 'banana'], ['Alice', 'Bob', 'Carol', 'David'], ['dogs', 'cats', 'moose', 'goose']] 我想显示为 apples Alice dogs oranges Bob cats cherries Carol moose banana David goose
tableData = [['apples', 'oranges', 'cherries', 'banana'], ['Alice', 'Bob', 'Carol', 'David'], ['dogs', 'cats', 'moose', 'goose']]
我想显示为
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
使条目右对齐。我已经看过了,但我无法实现类似的结果。到目前为止,我掌握的代码是:
tableData = [['apples', 'oranges', 'cherries', 'banana'], ['Alice', 'Bob', 'Carol', 'David'], ['dogs', 'cats', 'moose', 'goose']]
total_len= [[] for x in range(len(tableData))]
longest_string = []
for y1 in range(0, len(tableData)):
for y2 in range(0, len(tableData[y1])):
total_len[y1].append(len(tableData[y1][y2]))
for y1 in range(0, len(total_len)):
longest_string.append(max(total_len[y1]))
for y1 in range(len(tableData)):
for y2 in range(len(tableData[y1])):
print("".join(tableData[y1][y2].rjust(longest_string[y1])))
以及链接线程中的:
>>> for row in zip(*tableData):
... print("{: >10} {: >10} {: >10}".format(*row))
...
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
虽然我真的更喜欢。我不知道你有没有,但如果你有,很容易做到。您可以创建dataframe,然后使用以下方法:
没有第三方熊猫的类似格式:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
# Find the max length of the word in each row
lens = [max(len(col) for col in row) for row in tableData]
# zip(*list) transposes a list...rows become columns
for row in zip(*tableData):
# Pass the column widths dynamically.
print('{:>{lens[0]}} {:>{lens[1]}} {:>{lens[2]}}'.format(*row,lens=lens))
输出:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
编辑
以下是一个可以动态显示任意数量行和列的版本:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
# Find the max length of the word in each row
lens = [max(len(col) for col in row) for row in tableData]
# build a format string with an entry for each column
rowfmt = '{:>{}} ' * len(tableData)
# zip(*list) transposes a list...rows become columns
for row in zip(*tableData):
# Pass the values and column widths dynamically.
# The zip pairs up each datum with its column width, but in tuples.
# For example, [data1,data2],[width1,width2] -> [(data1,width1),(data2,width2)]
# itertools.chain flattens the list of tuples.
# For example, above becomes [data1,width1,data2,width2]
print(rowfmt.format(*itertools.chain(*zip(row,lens))))
我认为在for the print语句的循环中有3个{:>{lens[I]}形式的条目,因为在这个示例特定的表数据中有3个子列表。是否有可能拥有某种功能,可以获取包含任意数量子列表的任何列表,并以相同的方式显示它?@user49231,是的,需要更多的工作和更复杂的代码。我将添加一个更灵活的示例。
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
# Find the max length of the word in each row
lens = [max(len(col) for col in row) for row in tableData]
# build a format string with an entry for each column
rowfmt = '{:>{}} ' * len(tableData)
# zip(*list) transposes a list...rows become columns
for row in zip(*tableData):
# Pass the values and column widths dynamically.
# The zip pairs up each datum with its column width, but in tuples.
# For example, [data1,data2],[width1,width2] -> [(data1,width1),(data2,width2)]
# itertools.chain flattens the list of tuples.
# For example, above becomes [data1,width1,data2,width2]
print(rowfmt.format(*itertools.chain(*zip(row,lens))))