[英]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. 如何循环检查坐标是否与另一个坐标不同。
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', ' ', ' ']]
#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 ==
list
中list
索引== 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.