How to stack indices and given values

后端 未结 3 1015
遥遥无期
遥遥无期 2021-01-28 17:04
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 get_num_va         


        
相关标签:
3条回答
  • 2021-01-28 17:09

    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='')
    
    0 讨论(0)
  • 2021-01-28 17:26

    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|
    
    0 讨论(0)
  • 2021-01-28 17:31

    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()
    
    0 讨论(0)
提交回复
热议问题