嵌套列表显示在列中

时间:2015-12-30 06:26:14

标签: 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

以便条目右对齐。我已经看过Create nice column output in python,但我无法实现类似的结果。我到目前为止的代码是:

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])))

3 个答案:

答案 0 :(得分:3)

来自链接主题的

zip()solution

>>> for row in zip(*tableData):
...     print("{: >10} {: >10} {: >10}".format(*row))
... 
    apples      Alice       dogs
   oranges        Bob       cats
  cherries      Carol      moose
    banana      David      goose

虽然我真的很喜欢"pandas" dataframe based solution

答案 1 :(得分:1)

我不知道你有pandas但如果你这样做很容易。您可以创建数据框,然后使用to_string方法:

$target_url="http://me.dealintech.com/partho.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
$file_contents=curl_exec($ch);
include('simple_html_dom.php');
$html = str_get_html($file_contents); 
$elem = $html->find('div[id=basic_info_content]');
echo "var_dump output: ";
var_dump($elem);

答案 2 :(得分:1)

没有第三方熊猫的类似格式:

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))))