问题:将数组中的4替换为7
kk = [[2, 3, 4],
[3, 4, 5],
[4, 5, 6]]
方法一:numpy
import numpy as np
# list 转 np
aaa = np.array(kk)
aaa[np.where(aaa==4)]=7
# np 转 list
kk = aaa.tolist()
print(kk)
输出:
[[2, 3, 7],
[3, 7, 5],
[7, 5, 6]]
方法二:pandas
import pandas as pd
import numpy as np
# list 转 pd
df = pd.DataFrame(kk, columns=['col1', 'col2', 'col3'])
new_df = df.replace({4: 7})
# pd 转 list
kk = new_df.to_numpy().tolist()
print(kk)
输出:
[[2, 3, 7],
[3, 7, 5],
[7, 5, 6]]