How to stack indices and given values

╄→尐↘猪︶ㄣ 提交于 2019-12-02 08:20:30

Your question is not super clear, but from what I understand, this is what you are looking for:

>>> def myPrint(L):
...   maxLen = max(len(str(n)) for n in L)
...   print ("|", "|".join(" "*(maxLen-len(str(i))) + str(i) for i in range(len(L))) + "|")
...   print ("|", "|".join(" "*(maxLen-len(str(n))) + str(n) for n in L) + "|")
... 
>>> myPrint([5, 40, 180, 13, 30, 20, 25, 24, 29,  31,  54, 46,  42, 50,  67, 17,
...            76, 33, 84, 35, 100, 37, 110, 32, 112, 15, 123, 3, 130, 42])
|   0|  1|  2|  3|  4|  5|  6|  7|  8|  9| 10| 11| 12| 13| 14| 15| 16| 17| 18| 19| 20| 21| 22| 23| 24| 25| 26| 27| 28| 29|
|   5| 40|180| 13| 30| 20| 25| 24| 29| 31| 54| 46| 42| 50| 67| 17| 76| 33| 84| 35|100| 37|110| 32|112| 15|123|  3|130| 42|

Your code seems very complex for what you're doing. You used the tag for-loop but you're using a while loop- I would recommend going with that for loop. You can loop over seq1_values, printing out all the indices, and then print a newline, then go through one more time and print all the values:

seq1_values = [5, 40, 180, 13, 30, 20, 25, 24, 29,  31,  54, 46,  42, 50,  67, 17,
           76, 33, 84, 35, 100, 37, 110, 32, 112, 15, 123, 3, 130, 42]


def main():
    for s in range(0,len(seq1_values)):
        print("|",str(s).rjust(3),end="")
    print("")                                   # Gives us a new line
    for s in range(0,len(seq1_values)):
        print("|",str(seq1_values[s]).rjust(3),end="")

main()

You are making this a lot more complex than it needs to be. It smells like homework, so I am going to help you along for the stacking portion, but it still needs to be justified after this, and this does not add a pipe character at the end, so you will have to figure that out on your own:

Something like this is all that you need for this in Python3 for stacking it like you need it:

for idx in range(0, len(seq1_values)):
    if idx == len(values) - 1:
        print('| ', idx)
    else:
        print('| ', idx, end='')

for val in len(seq1_values):
    print('| ', val, end='')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!