简体   繁体   English

如何访问2d数组的每个元素,然后替换它们?

[英]How to get access to every element of the 2d array and than replace them?

arr = [['.' for i in range(4)] for j in range(4)]

for line, i in enumerate(arr):
    for column, j in enumerate(i):
        print(j, 'at column', column+1, 'line', line+1) # we can know which  
                                                        # postition takes 
                                                        # every element

How out of loop to check if coordinate is different with another coordinate. 如何循环检查坐标是否与另一个坐标不同。

What I want to get in final: 我想最终得到什么:

pseudocode: 伪代码:

#arr[x][y]
arr[1][0] = 'new'
if arr[1][4] - arr[1][0] == 4: # i.e. coord are different by `y` on 4 pos 
    arr[1][4] = 'new'`

               # Before || After
[[' ', 'new', ' ', ' '],||  [[' ', ' ', ' ', ' '],
 [' ', ' ', ' ', ' '],   ||   [' ', ' ', ' ', ' '],
 [' ', ' ', ' ', ' '],   ||   [' ', ' ', ' ', ' '],
 [' ', ' ', ' ', ' '],   ||   [' ', ' ', ' ', ' '],
 [' ', ' ', ' ', ' ']]   ||   [' ', 'new', ' ', ' ']]

OR 要么

#arr[x][y]
arr[0][0] = 'new'
if arr[3][0] - arr[0][0] == 3: # i.e. coord are different by `x` on 3 pos 
    arr[3][0] = 'new'`

               # Before || After
[['new', ' ', ' ', ' '],||  [[' ', ' ', ' ', 'new'],
 [' ', ' ', ' ', ' '],   ||   [' ', ' ', ' ', ' '],
 [' ', ' ', ' ', ' '],   ||   [' ', ' ', ' ', ' '],
 [' ', ' ', ' ', ' '],   ||   [' ', ' ', ' ', ' '],
 [' ', ' ', ' ', ' ']]   ||   [' ', ' ', ' ', ' ']]

Necessarily need to know which list in main list takes position, but how it does outside of loop without numpy, using native python? 必要地需要知道主列表中哪个列表的位置,但是如何使用本机python在没有numpy的情况下在循环之外执行呢?

Question : How out of loop to check if coordinate is different with another coordinate. 问题 :如何循环检查坐标是否与另一个坐标不同。

Define the coordinate's using tuple , then compare the tuples : 使用tuple定义坐标,然后比较tuples

RC = (1,4)
RC2 = (1,0)

if RC == RC2:
    print('Equal')
else:
    print('Different')

Type list is 0 Based You have a list of lists. 类型list是基于0的您有一个列表列表。

RC Coordinate System, the same as with Excel RC坐标系,与Excel相同
R == index of list in list == y == Row R == listlist索引== y ==行
C == index inside a list == x == Column C == list内的索引== x ==列

        A    B    C    D
C =>     0    1    2    3
    ----------------------
R:0 | [0.0, 0.1, 0.2, 0.3]
R:1 | [1.0, 1.1, 1.2, 1.3]
R:2 | [2.0, 2.1, 2.2, 2.3]
R:3 | [3.0, 3.1, 3.2, 3.3]

Use the opposite 使用相反的

#arr[y][x]

The first index=y is the index of a list in arr , alias row . 第一个index = y是arr别名row中的list的索引。
The second index=x is the index inside the list selected with y , alias column . 第二索引= x是内部的索引list与所选择y ,别名

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

相关问题 如何获得二维数组中的精灵? - How to get an access to sprite in 2d array? 如何从二维numpy数组的每一行中获取元素的首次出现? - how to get the first occurring of an element from every row in a 2D numpy array? 如何为二维数组的指定列中的每个元素添加一个值? - How to add a value to every element in a specified column of a 2D array? 如何将2D数组中的每个元素乘以-1? - How do you multiply every element inside a 2D array by -1? 将二维数组中的每个元素重复为二维 output - Repeat every element in a 2D array to a 2D output 如何在以下场景中访问2D数组的元素? - How do I access the element of a 2D array in the following scenario? “python way”解析并有条件地替换2D列表中的每个元素 - “python way” to parse and conditionally replace every element in 2D list 如何将每个图像加载到文件中并将其转换为2D数组,然后将其导出到新文件夹? - How do I load every image in a file and convert them to 2D array then export it to a new folder? 如何按元素将一维数组添加到二维数组以在 numpy 中获取 3d 数组 - How to add a 1d array to a 2d array element-wise to get a 3d array in numpy 获取从 2 x 1D arrays 构建的 2D 数组元素的访问索引 - Get access index of a 2D array element built from 2 x 1D arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM