如何堆叠索引和给定值 [英] How to stack indices and given 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 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|
推荐答案
您正在使它变得比所需的复杂得多.它闻起来像是作业,所以我将帮助您进行堆栈部分的工作,但是在此之后仍然需要证明它是正确的,并且这不会在末尾添加竖线字符,因此您必须在自己的代码上弄清楚这一点.自己的:
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:
在Python3中,您需要的只是这样的东西:像您需要的那样将其堆叠:
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='')
这篇关于如何堆叠索引和给定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文