简体   繁体   English

需要帮助将字符串中的三角形转换为数组

[英]need help converting a triangle in string to array

triangle = """
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23"""

def return_triangle(triangle,n):
    arr_start = triangle.split()
    print(arr_start)
    current_row = 1
    start_index = current_row - 1
    max_index = start_index + current_row
    triangle_array = []
    while current_row <= rows:
        print('len arr_start',len(arr_start))
        temp_arr = []
        for i in range(start_index,max_index):
            temp_arr.append(arr_start[i])
        triangle_array.append([temp_arr])
        current_row += 1
        start_index += current_row - 1
        max_index = start_index + current_row
    return triangle_array

print(return_triangle(triangle))

here is my output 
[['63', '66', '04', '68', '89', '53', '67', '30', '73', '16', '69', '87', '40', '31']]
12
[['91', '71', '52', '38', '17', '14', '91', '43', '58', '50', '27', '29', '48']]
11
[['70', '11', '33', '28', '77', '73', '17', '78', '39', '68', '17', '57']]
10
[['53', '71', '44', '65', '25', '43', '91', '52', '97', '51', '14']]
9
[['41', '48', '72', '33', '47', '32', '37', '16', '94', '29']]
8
[['41', '41', '26', '56', '83', '40', '80', '70', '33']]
7
[['99', '65', '04', '28', '06', '16', '70', '92']]
6
[['88', '02', '77', '73', '07', '63', '67']]
5
[['19', '01', '23', '75', '03', '34']]
4
[['20', '04', '82', '47', '65']]
3
[['18', '35', '87', '10']]
2
[['17', '47', '82']]
1
[['95', '64']]
0
[['75']]
0

i get up to the last n-2 rows but i cannot get the last 2 rows to print i want to know how to do that withut going out of bound in my loop.我最多打印到最后 n-2 行,但无法打印最后 2 行,我想知道如何在不超出循环范围的情况下执行此操作。 Now if i look at my expected output which is supposed to be现在,如果我看看我的预期输出应该是

[[04, 62, 98, 27, 23, 09, 70, 98, 73, 93, 38, 53, 60, 04, 23]] 13 [['63', '66', '04', '68', '89', '53', '67', '30', '73', '16', '69', '87', '40', '31']] 12 [['91', '71', '52', '38', '17', '14', '91', '43', '58', '50', '27', '29', '48']] 11 [['70', '11', '33', '28', '77', '73', '17', '78', '39', '68', '17', '57']] 10 [['53', '71', '44', '65', '25', '43', '91', '52', '97', '51', '14']] 9 [['41', '48', '72', '33', '47', '32', '37', '16', '94', '29']] 8 [['41', '41', '26', '56', '83', '40', '80', '70', '33']] 7 [['99', '65', '04', '28', '06', '16', '70', '92']] 6 [['88', '02', '77', '73', '07', '63', '67']] 5 [['19', '01', '23', '75', '03', '34']] 4 [['20', '04', '82', '47', '65']] 3 [['18', '35', '87', '10']] 2 [['17', '47', '82']] 1 [['95', '64']] 0 [['75']] 0 [[04, 62, 98, 27, 23, 09, 70, 98, 73, 93, 38, 53, 60, 04, 23]] 13 [['63', '66', '04', '68 ', '89', '53', '67', '30', '73', '16', '69', '87', '40', '31']] 12 [['91', '71', '52', '38', '17', '14', '91', '43', '58', '50', '27', '29', '48']] 11 [['70', '11', '33', '28', '77', '73', '17', '78', '39', '68', '17', '57'] ] 10 [['53', '71', '44', '65', '25', '43', '91', '52', '97', '51', '14']] 9 [['41', '48', '72', '33', '47', '32', '37', '16', '94', '29']] 8 [['41', '41', '26', '56', '83', '40', '80', '70', '33']] 7 [['99', '65', '04', '28 ', '06', '16', '70', '92']] 6 [['88', '02', '77', '73', '07', '63', '67'] ] 5 [['19', '01', '23', '75', '03', '34']] 4 [['20', '04', '82', '47', '65' ']] 3 [['18', '35', '87', '10']] 2 [['17', '47', '82']] 1 [['95', '64'] ] 0 [['75']] 0

now this is what i want but i cant seem to get this现在这就是我想要的,但我似乎无法得到这个

  listOfStrings = [ i.split() for i in triangle.splitlines() if i ]
  listOfInt = []
  for row in listOfStrings:
      listOfInt.append( [ int(x) for x in row ] )

First:第一的:

For me it works if you do the following:对我来说,如果您执行以下操作,它会起作用:

def return_triangle(triangle,n):

change to:改成:

def return_triangle(triangle,rows):

Or change the rows in the while loop to n.或者将 while 循环中的行更改为 n。 That's an error that don't even let you run the code so maybe some copy paste error.这是一个错误,甚至不允许您运行代码,因此可能存在一些复制粘贴错误。

Second:第二:

You have an error in your for loop.你的 for 循环有错误。 I don't know why you do -1 but it should look like this and it works:我不知道你为什么这样做 -1 但它应该看起来像这样并且它有效:

for i in reversed(range(len(arr_triangle)-1)):

to

for i in reversed(range(len(arr_triangle))):

Don't forget the index starts with 0.不要忘记索引从 0 开始。

The output would look like this:输出将如下所示:

14
[['04', '62', '98', '27', '23', '09', '70', '98', '73', '93', '38', '53', '60', '04', '23']]
13
[['63', '66', '04', '68', '89', '53', '67', '30', '73', '16', '69', '87', '40', '31']]
12
[['91', '71', '52', '38', '17', '14', '91', '43', '58', '50', '27', '29', '48']]
11
[['70', '11', '33', '28', '77', '73', '17', '78', '39', '68', '17', '57']]
10
[['53', '71', '44', '65', '25', '43', '91', '52', '97', '51', '14']]
9
[['41', '48', '72', '33', '47', '32', '37', '16', '94', '29']]
8
[['41', '41', '26', '56', '83', '40', '80', '70', '33']]
7
[['99', '65', '04', '28', '06', '16', '70', '92']]
6
[['88', '02', '77', '73', '07', '63', '67']]
5
[['19', '01', '23', '75', '03', '34']]
4
[['20', '04', '82', '47', '65']]
3
[['18', '35', '87', '10']]
2
[['17', '47', '82']]
1
[['95', '64']]
0
[['75']]
0  # Output of max_sum

Since max_sum is 0 the last 0 is from maxsum the output before is from your for loop.由于 max_sum 是 0 最后一个 0 来自 maxsum 之前的输出来自您的 for 循环。

As a remark you can see that you have [['75']] => a list in a list.作为备注,您可以看到您有 [['75']] => 列表中的列表。

You can eliminate this if you change this:如果你改变这个,你可以消除这个:

triangle_array.append([temp_arr])

to

triangle_array.append(temp_arr)

This is a more advanced solution as it is a list comprehension within a list comprehension but shows the power of Python when you get up and runnig with it.这是一个更高级的解决方案,因为它是列表推导式中的列表推导式,但在您启动并运行它时显示了 Python 的强大功能。

Given your definition of name triangle above, the following code shows how it can be easily parsed into a list of lists of ints:鉴于您对上面的名称triangle的定义,以下代码显示了如何轻松地将其解析为整数列表的列表:

In [1]: [[int(item) for item in row.strip().split()]
   ...:  for row in triangle.strip().splitlines()]
Out[1]: 
[[75],
 [95, 64],
 [17, 47, 82],
 [18, 35, 87, 10],
 [20, 4, 82, 47, 65],
 [19, 1, 23, 75, 3, 34],
 [88, 2, 77, 73, 7, 63, 67],
 [99, 65, 4, 28, 6, 16, 70, 92],
 [41, 41, 26, 56, 83, 40, 80, 70, 33],
 [41, 48, 72, 33, 47, 32, 37, 16, 94, 29],
 [53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14],
 [70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57],
 [91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48],
 [63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31],
 [4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23]]

In [2]: 

both the uses of, (just), .strip() above could be deleted for your example string but I usually use them as it allows for spurious blank lines and spaces at the end, (and beginning), of lines.上面的 (just), .strip()两种用法都可以为您的示例字符串删除,但我通常使用它们,因为它允许在行的末尾(和开头)出现虚假的空白行和空格。

Explanation解释

The outer loop is the second line of the comprehension which assigns local variable row to successive lines of triangle.外循环是推导式的第二行,它将局部变量row分配给三角形的连续线。 The inner loop is shown as the first line of the comprehension and produces the inner list by splitting each row on whitespace and converting the resultant item to an int.内部循环显示为推导式的第一行,并通过在空白处拆分每一row并将结果item转换为 int 来生成内部列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM