Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 漂亮的打印(2D阵列、长方体)_Python_Arrays_Multidimensional Array - Fatal编程技术网

Python 漂亮的打印(2D阵列、长方体)

Python 漂亮的打印(2D阵列、长方体),python,arrays,multidimensional-array,Python,Arrays,Multidimensional Array,我编写了以下代码: for row in range(len(listOfLists)): print('+' + '-+'*len(listOfLists)) print('|', end='') for col in range(len(listOfLists[row])): print(listOfLists[row][col], end='|') print(' ') #To change lines print('+' + '-+'*l

我编写了以下代码:

for row in range(len(listOfLists)):
    print('+' + '-+'*len(listOfLists))
    print('|', end='')
    for col in range(len(listOfLists[row])):
        print(listOfLists[row][col], end='|')
    print(' ') #To change lines 
print('+' + '-+'*len(listOfLists))
输入:

输出:

期望输出:

它在二维数组的元素周围打印一个'+-+'。 但是,我的代码只适用于方形数组(n^2)

如何对其进行推广,使其适用于数组的任何变化(只要所有列表长度相等)


谢谢

您的问题是
len(列表)
用于两个方向的打印表大小
len(listOfLists)
默认为行数,通过执行
len(listOfLists[0])
可以获得列数

 listOfLists = [['a', 'b', 'c'],
     ['d', 'e', 'f'],
     ['g', 'h', 'i'],
     ['j', 'k', 'l']]

for row in range(len(listOfLists)):
    print('+' + '-+'*len(listOfLists[0]))
    print('|', end='')
    for col in range(len(listOfLists[row])):
        print(listOfLists[row][col], end='|')
    print(' ') #To change lines 
print('+' + '-+'*(len(listOfLists[0])))
输出:

+-+-+-+
|a|b|c| 
+-+-+-+
|d|e|f| 
+-+-+-+
|g|h|i| 
+-+-+-+
|j|k|l| 
+-+-+-+
快乐编码

def awesome_print(listOfLists):
    for row in range(len(listOfLists)):
        print('+' + '-+'*len(listOfLists[row]))
        print('|', end='')
        for col in range(len(listOfLists[row])):
            print(listOfLists[row][col], end='|')
        print(' ') #To change lines 
    print('+' + '-+'*len(listOfLists[row]))

awesome_print([[1,2,3], [1,2,3], [2,3,0], [2,3,4]])
输出

+-+-+-+
|1|2|3| 
+-+-+-+
|1|2|3| 
+-+-+-+
|2|3|0| 
+-+-+-+
|2|3|4| 
+-+-+-+
如果您需要使用非固定大小的子阵列打印数据

def awesome_print2(listOfLists):
    for row in range(len(listOfLists)):
        print('+' + '-+'*len(listOfLists[row]))
        print('|', end='')
        for col in range(len(listOfLists[row])):
            print(listOfLists[row][col], end='|')
        print()
        print('+' + '-+'*len(listOfLists[row]))
awesome_print2([[1,2,3,5], [1,2,3], [2,3,0,6,3], [2,3,4]])
输出:

+-+-+-+-+
|1|2|3|5|
+-+-+-+-+
+-+-+-+
|1|2|3|
+-+-+-+
+-+-+-+-+-+
|2|3|0|6|3|
+-+-+-+-+-+
+-+-+-+
|2|3|4|
+-+-+-+

您正在根据行数而不是列数打印此分隔符。使用其他测试用例有助于立即进行调试

def printListOfLists(listOfLists):
    for row in range(len(listOfLists)):
        print('+' + '-+' * len(listOfLists[0]))
        print('|', end='')
        for col in range(len(listOfLists[row])):
            print(listOfLists[row][col], end='|')
        print(' ')  # To change lines
    print('+' + '-+' * len(listOfLists[0]))


printListOfLists([
    ['a', 'b', 'c'],
    ['d', 'e', 'f'],
    ['g', 'h', 'i'],
    ['j', 'k', 'l']
])

printListOfLists([['a', 'b', 'c']])

printListOfLists([['a'], ['b'], ['c']])
现在预期所有产生的结果:

+-+-+-+
|a|b|c| 
+-+-+-+
|d|e|f| 
+-+-+-+
|g|h|i| 
+-+-+-+
|j|k|l| 
+-+-+-+


+-+-+-+
|a|b|c| 
+-+-+-+


+-+
|a| 
+-+
|b| 
+-+
|c| 
+-+

请缩进你的代码,给你,请,正确的格式代码,并举例说明你得到什么和你想要什么?@vishes_shell太:-)@345243lkj对不起!!!我修好了!:)@vishes_shell很抱歉格式不清楚。我现在修好了!:)这似乎在每行之间有一个额外的空间。最后一个print语句应该在for循环之外,以避免出现这种情况(除非这是有意的)。非常感谢您的帮助!:)抢手货固定帐户that@345243lkj非常感谢!:)非常感谢你的帮助!
+-+-+-+-+
|1|2|3|5|
+-+-+-+-+
+-+-+-+
|1|2|3|
+-+-+-+
+-+-+-+-+-+
|2|3|0|6|3|
+-+-+-+-+-+
+-+-+-+
|2|3|4|
+-+-+-+
def printListOfLists(listOfLists):
    for row in range(len(listOfLists)):
        print('+' + '-+' * len(listOfLists[0]))
        print('|', end='')
        for col in range(len(listOfLists[row])):
            print(listOfLists[row][col], end='|')
        print(' ')  # To change lines
    print('+' + '-+' * len(listOfLists[0]))


printListOfLists([
    ['a', 'b', 'c'],
    ['d', 'e', 'f'],
    ['g', 'h', 'i'],
    ['j', 'k', 'l']
])

printListOfLists([['a', 'b', 'c']])

printListOfLists([['a'], ['b'], ['c']])
+-+-+-+
|a|b|c| 
+-+-+-+
|d|e|f| 
+-+-+-+
|g|h|i| 
+-+-+-+
|j|k|l| 
+-+-+-+


+-+-+-+
|a|b|c| 
+-+-+-+


+-+
|a| 
+-+
|b| 
+-+
|c| 
+-+