How to stack indices and given values

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 16:59:30

问题


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_values():
    global seq1_values
    return len( seq1_values )

def get_values():
    global seq1_values
    for value in seq1_values:
        yield value
    yield None

def next_value():
    return next( next_value.values )
next_value.values = get_values()

def main():
    numbers = []
    value = next_value()
    while value is not None:
        numbers.append(value)
        input_size = len(numbers)-1
        for i in range(input_size):
            print("|", str(input_size).rjust(4), end="")
        for j in range(input_size):
            print("|", str(value).rjust(4), end="")
            value = next_value()
main()

Output is supposed to look like this:

|    0|    1|    2|    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|    30|

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

回答1:


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|



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/19217203/how-to-stack-indices-and-given-values

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