Python 在嵌套循环和矩阵中更新数值。

huangapple go评论72阅读模式
英文:

Python updating values within a nested loop and matrix

问题

我这里有一个嵌套循环,我怎么也搞不清楚。在一个矩阵中,如果 i 的值可以被 10 整除,它应该将 ends1 和 ends2 的值增加 10。然而,当它进入 else 块时,这些值没有被更新。这应该是一个基础问题,但我搞不清楚。任何帮助将不胜感激。

更新:此函数接受一个二维列表/矩阵。还更新了代码以使其正常工作。

在更新后的片段中,如果你运行代码并输入 21,else 块中的 ends1 或 ends2 不会增加 10。该代码仅适用于可以被 10 整除加 1 的输入(例如,11、21、31 等)。

inputs = int(input('输入行和列的数量:'))
n = inputs
m = inputs
start_time = time.time()
Matrix = [[0 for x in range(n)] for y in range(m)]

# 填充可以被 10 整除的坐标
def terrain_inter(M):
    # 填充与可以被 10 整除的坐标对齐的垂直坐标
    for i in range(len(M)):
        ends1 = 0
        ends2 = 10
        for j in range(len(M)):
            if i == 0:
                continue
            elif i % 10 == 0:
                ends1 = i - 10
                ends2 = i
                print(ends2)
            else:
                print(ends1)
                M[i][j] = M[ends1][j] + ((i - ends1) / (ends2 - ends1)) * (M[ends2][j] - M[ends1][j])
                M[i][j] = round(M[i][j], 2)
    for row in M:
        print(*row)
terrain_inter(Matrix)

如果输入是 21,那么第一行中的 ends1 是 0,即使它应该在达到 (10, 0) 时变成 10。

Python 在嵌套循环和矩阵中更新数值。

英文:

I have here a nested loop that I cannot figure out for the life of me. In a matrix, if the value of i is divisible by 10, it should add the value of ends1 and ends2 by 10. However, when it reaches the else block, the values are not updated. This is supposed to be an elementary problem but I cannot figure it out. Any help would be appreciated.

update: this function accepts a 2d list/matrix. Also updated code to make it work

In the updated snippet, if you were to run the code, and input 21, the ends1 or ends2 in the else block does not add 10. The code only works for inputs that are divisible by 10+1 (e.g. 11, 21, 31, etc.)

inputs = int(input ('Enter number of rows and columns: '))
n = inputs
m = inputs
start_time = time.time()
Matrix = [[0 for x in range(n)] for y in range(m)] 

#populate coordinates divisible by 10

def terrain_inter(M):
	
	#populate vertical coordinates that are aligned with those coordinates are divisible by 10
	for i in range(len(M)):
		ends1 = 0
		ends2 = 10
		for j in range (len(M)):
			if i == 0:
				continue
			elif i % 10 == 0:
				ends1 = i - 10
				ends2 = i
				print(ends2)
			else:
                    print(ends1)
    				M[i][j] = M[ends1][j] + ((i-ends1)/(ends2 - ends1)) * (M[ends2][j] - M[ends1][j])
    				M[i][j] = round(M[i][j],2)


	for row in M:
	   print(*row)
terrain_inter(Matrix)

results if input is 21; first line is ends1 which is 0 even if it shouldve been 10 once it reached (10,0)

Python 在嵌套循环和矩阵中更新数值。

答案1

得分: 1

看起来你每次迭代下一行/列时都在重置ends1ends2

建议:

  • ends1ends2的初始化移到循环外面。
  • 如果你只想要添加10(如你在注释中指定的),就不要使用i
  • 不再计算len(M)(因为你在编辑问题时不再适用)。
  • 从你的实现中去掉mn,因为它们是相同的。
  • 请停止编辑问题,谢谢...
ends1 = 0
ends2 = 10
for i in range(len(M)):
    for j in range(len(M[0])):
        if i % 10 == 0 and i > 0:
            ends1 += 10
            ends2 += 10
            print(ends2)
        else:
            M[i][j] = M[ends1][j] + ((i - ends1) / (ends2 - ends1)) * (M[ends2][j] - M[ends1][j])
            M[i][j] = round(M[i][j], 2)
英文:

Looks like you're resetting the ends1 and ends2 every time you iterate over next row/column.

Suggestions:

  • Move initialization of ends1 and ends2 out of the loop
  • Don't use i if you only want to add 10 (as you specified in the comment)
  • <s>Calculate the len(M) once</s> (doesn't apply anymore since you are editing the question)
  • Get rid of either m or n since they are the same in your implementation
  • stop editing the question, please...
ends1 = 0
ends2 = 10
for i in range(len(M)):
    for j in range (len(M[0])):
        if i % 10 == 0 and i&gt;0:
            ends1 += 10
            ends2 += 10
            print(ends2)
        else:
            M[i][j] = M[ends1][j] + ((i-ends1)/(ends2 - ends1)) * (M[ends2][j] - M[ends1][j])
            M[i][j] = round(M[i][j],2)

答案2

得分: 0

如果`i`代表行,`j`代表列那么它应该是这样的

```python
for i in range(len(M)):
    ends1 = 0
    ends2 = 10
    for j in range(len(M[0])):
        if i == 0:
            continue
        elif i % 10 == 0:
            ends1 = i - 10
            ends2 = i
            print(ends2)
        else:
            M[i][j] = M[ends1][j] + ((i-ends1)/(ends2 - ends1)) * (M[ends2][j] - M[ends1][j])
            M[i][j] = round(M[i][j],2)

在给定的代码中,仅当列数等于行数时才有效。


<details>
<summary>英文:</summary>

If `i` for row and `j` for column, it should be something like

```python
for i in range(len(M)):
    ends1 = 0
    ends2 = 10
    for j in range (len(M[0])):
        if i == 0:
            continue
        elif i % 10 == 0:
            ends1 = i - 10
            ends2 = i
            print(ends2)
        else:
            M[i][j] = M[ends1][j] + ((i-ends1)/(ends2 - ends1)) * (M[ends2][j] - M[ends1][j])
            M[i][j] = round(M[i][j],2)

In given code it works only when number of columns is equal to number of rows.

答案3

得分: 0

ends1 = 0
ends2 = 10
for i in range(len(M)):
    for j in range(len(M)):
        if i == 0:
            continue
        elif i % 10 == 0 and j == len(M)-1:
            ends2 += 10
            ends1 += 10
            print(ends1)
        else:
            M[i][j] = M[ends1][j] + ((i-ends1)/(ends2 - ends1)) * (M[ends2][j] - M[ends1][j])
            M[i][j] = round(M[i][j], 2)

# Moved ends1 and ends2 outside of loop
# Added `j == len(M)-1` to elif block to ensure that both ends are added by 10
英文:
ends1 = 0
ends2 = 10
for i in range(len(M)):
	for j in range (len(M)):
		if i == 0:
			continue
		elif i % 10 == 0 and j == len(M)-1:
			ends2 += 10
			ends1 += 10
			print(ends1)
		else:
			M[i][j] = M[ends1][j] + ((i-ends1)/(ends2 - ends1)) * (M[ends2][j] - M[ends1][j])
			M[i][j] = round(M[i][j],2)
  • Moved ends1 and ends2 outside of loop
  • added j == len(M)-1 to elif block to ensure that both ends are added by 10

huangapple
  • 本文由 发表于 2023年3月7日 16:08:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定