英文:
Changing Specific Values In a 2D array Using For Loops and If statements without a known Index
问题
我有一个来自geotiff文件的2D数组。请参阅下面的示例数组:
example_array = [[0, 1, 1, 0, 0, 0, 0],
[2, 1, 1, 2, 0, 0, 0],
[0, 3, 1, 1, 3, 0, 0],
[4, 1, 1, 4, 0, 0, 0],
[0, 5, 1, 1, 5, 0, 0],
[0, 1, 1, 0, 0, 0, 0]]
我正在尝试编写一个程序,将每个嵌套列表中大于1的特定数字更改为'value + 100'。
在他人的帮助下,请查看下面的工作代码片段,它正确地将每个列表中第一个大于1的值更改为值+100。
for l in example_array:
for i, x in enumerate(l):
if x > 1:
l[i] -= 100
break
如果我想要专门更改每个列表中大于1的第二个值,我尝试了下面的不正确代码片段:
for l in example_array:
for i, x in enumerate(l):
if x > 1:
if x > 1:
l[i] -= 100
break
这里的思路是,如果我在for循环
中添加另一个if x > 1:
条件,它将从第一个大于1的数字开始,然后移动到下一个if x > 1:
中大于1的第二个值。但在这里不是这种情况。有人有关于如何正确更改第二个大于1的数字的建议,以便显示下面的正确example_array
吗?
example_array = [[0, 1, 101, 0, 0, 0, 0],
[2, 101, 1, 2, 0, 0, 0],
[0, 3, 101, 1, 3, 0, 0],
[4, 101, 1, 4, 0, 0, 0],
[0, 5, 101, 1, 5, 0, 0],
[0, 1, 101, 0, 0, 0, 0]]
英文:
I have a 2D array from a geotiff file. See the example array below:
example_array = [[ 0, 1, 1, 0, 0, 0, 0],
[ 2, 1, 1, 2, 0, 0, 0],
[ 0, 3, 1, 1, 3, 0, 0],
[ 4, 1, 1, 4, 0, 0, 0],
[ 0, 5, 1, 1, 5, 0, 0],
[ 0, 1, 1, 0, 0, 0, 0]]
I am attempting to write a program that will change the specific number greater than 1 in each nested list to the 'value + 100'.
With the help of others see the below working code snippet that correctly changes the first value in each list greater than 1 to +100 of the value.
for l in example_array:
for i, x in enumerate(l):
if x > 1:
l[i] -= 100
break
If I wanted to change exclusively the second value in each list greater than 1 I attempted the below incorrect code snippet:
for l in example_array:
for i, x in enumerate(l):
if x > 1:
if x > 1:
l[i] -= 100
break
The thought process here is that if I added another if x>1:
argument to the for loop
it would start at the first number greater than 1 then move to the second value greater than 1 with the next if x>1:
. That is not the case here. Does anyone have any suggestions on how to correctly change the second number greater that 1 such that is displays the correct example_array
below?:
example_array = [[ 0, 1, 101, 0, 0, 0, 0],
[ 2, 101, 1, 2, 0, 0, 0],
[ 0, 3, 101, 1, 3, 0, 0],
[ 4, 101, 1, 4, 0, 0, 0],
[ 0, 5, 101, 1, 5, 0, 0],
[ 0, 1, 101, 0, 0, 0, 0]]
答案1
得分: 0
我假设在你的问题中,">=1" 的意思是大于等于1,而不是大于1,根据你提供的最后一个例子。这不是最优雅的解决方案,但我想这可能是你想要的:
for l in example_array:
c = 0
for i, x in enumerate(l):
if x >= 1:
if c == 1:
l[i] += 100
break
c += 1
然而,你可以使用NumPy更高效地实现:
import numpy as np
# 假设example_array是一个列表的列表
for row in example_array:
row_np = np.array(row) # 将列表转换为NumPy数组以进行比较
positive_indices = np.where(row_np >= 1)[0] # 获取大于等于1的元素的索引
if len(positive_indices) >= 2: # 检查是否至少有两个大于等于1的元素
row[positive_indices[1]] += 100 # 在第二个大于等于1的元素上加100
英文:
I assumed for this response that you meant >=1 and not >1, judging by the final example you provided.
this is not the pretties solution but i guess it is what you're looking for:
for l in example_array:
c = 0
for i, x in enumerate(l):
if x >= 1:
if c == 1:
l[i] += 100
break
c += 1
However you can do it much more efficiently using numpy:
import numpy as np
# Assuming example_array is a list of lists
for row in example_array:
row_np = np.array(row) # Convert list to NumPy array for the comparison
positive_indices = np.where(row_np >= 1)[0] # Get indices of elements greater than or equal to 1
if len(positive_indices) >= 2: # Check if there are at least two elements greater than or equal to 1
row[positive_indices[1]] += 100 # Add 100 to the second element greater than or equal to 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论