【问题标题】:How to stack indices and given values如何堆叠索引和给定值
【发布时间】:2013-10-13 13:49:03
【问题描述】:
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()

输出应该是这样的:

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

【问题讨论】:

  • 什么不起作用?您调试脚本的尝试在哪里失败了?
  • 你真的需要为你的问题添加更多细节。他们是如何“关闭”的?究竟是什么不起作用?

标签: list python-3.3 indices


【解决方案1】:

你的问题不是很清楚,但据我了解,这就是你要找的:

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

【讨论】:

  • 你能在python3上写这个吗?这很难翻译,没有得到我想要的输出
  • @user88453:这是 python3。我已经从 python3 复制粘贴了一个终端会话
【解决方案2】:

您的代码看起来非常复杂。您使用了标签for-loop,但您使用的是while 循环——我建议您使用for 循环。您可以遍历 seq1_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】:

    你让这变得比它需要的复杂得多。它闻起来像家庭作业,所以我会在堆叠部分帮助你,但在此之后仍然需要证明它是合理的,而且这不会在最后添加管道字符,所以你必须在你的自己的:

    在 Python3 中你只需要这样的东西就可以像你需要的那样堆叠它:

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

    【讨论】:

    • 您的代码中几乎没有错误。 len(values),应该是len(seq1_values),for val in len(seq1_values):应该是for val in seq1_values:
    猜你喜欢
    • 1970-01-01
    • 2013-02-07
    • 2020-07-28
    • 2021-02-07
    • 2013-11-09
    • 2020-02-18
    • 1970-01-01
    • 2016-09-06
    相关资源
    最近更新 更多