简体   繁体   English

蟒蛇。 将新数字覆盖到矩阵的列中

[英]Python. Overwriting new numbers into a column in a matrix

I have data set which has 44000 rows and 5 columns. 我有44000行和5列的数据集。 What I want to do is to erase all the numbers in the second column and to put new numbers there in an incremental order but every other column remains the same as before. 我要执行的操作是擦除第二列中的所有数字,并以递增的顺序将新数字放在该位置,但每隔一列仍与以前相同。 For example, first row for that column is 0.001, second row 0.003 and the third row is 0.005 and so on. 例如,该列的第一行为0.001,第二行为0.003,第三行为0.005,依此类推。

How can I implement in Python code? 如何在Python代码中实现? I am still not quite skillful in Python. 我对Python仍然不太熟练。

I appreciate your helps in advance. 非常感谢您的帮助。

I will explain with an example, for now let me take this list. 我将举例说明,现在让我列出这个清单。 Your matrix can be thought of as a 2-d list. 您的矩阵可以视为二维列表。

let my list be L= [[0,0,0],[0,0,0],[0,0,0]]. 让我的列表为L = [[0,0,0],[0,0,0],[0,0,0]]。 This is 3x3 matrix. 这是3x3矩阵。 Assuming that The second column of every row needs to be uniformly incremented by 2, you can try this. 假设每行的第二列需要统一增加2,则可以尝试这样做。

l = [[0,0,0],[0,0,0],[0,0,0]]
inc=0

for i in l:
    i[1]=inc+2
    init=i[1]

print(l)

For every row in the matrix, modify the second column. 对于矩阵中的每一行,修改第二列。 Use a variable (incr) to track the increments. 使用变量(incr)跟踪增量。 At the first iteration, the second column of row 1 will be '2'. 在第一次迭代时,第1行的第二列将为'2'。 Once the value is modified, then update the increment variable to the new value, so that it will add your incremental value to this new value in the next iteration. 修改值后,将增量变量更新为新值,以便在下一次迭代中将增量值添加到该新值。 This is what happens. 这就是发生的情况。

Iteration 1: 迭代1:
row1 before modifying is [0,0,0]. 修改前的row1为[0,0,0]。 Then replace the second column with incr+2 (incr is 0 at first, so i[1] becomes 2). 然后用incr + 2替换第二列(incr首先为0,所以i [1]变为2)。 Then incr = i[1] will make incr = 2. So now row1 after modifying is [0,2,0]. 然后incr = i [1]将使incr =2。因此,现在修改后的row1为[0,2,0]。

Iteration 2: row2 before modifying is [0,0,0] . 迭代2:修改前的row2为[0,0,0]。 Then replace the second column with incr+2 (incr value changed to 2 in the previous iteration. So now i[1] is incr+2 which is 2+2=4). 然后将第二列替换为incr + 2(在上一次迭代中,incr值更改为2。因此,现在i [1]为incr + 2,即2 + 2 = 4)。 Then incr = i[1] will make incr=4. 然后incr = i [1]将使incr = 4。 So now, row2 after modifying is [0,4,0]. 因此,现在修改后的row2为[0,4,0]。

This keeps repeating. 这不断重复。

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

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