73. Set Matrix Zeroes python

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.

Example 1:

Input: 
[
  [1,1,1],
  [1,0,1],
  [1,1,1]
]
Output: 
[
  [1,0,1],
  [0,0,0],
  [1,0,1]
]

Example 2:

Input: 
[
  [0,1,2,0],
  [3,4,5,2],
  [1,3,1,5]
]
Output: 
[
  [0,0,0,0],
  [0,4,5,0],
  [0,3,1,0]
]

题意:把0所在行和所在列都变为0

思路:把0所在的坐标存起来,然后该行和列,都变为0

class Solution(object):
    def setZeroes(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: None Do not return anything, modify matrix in-place instead.
        """
        
        zero_set=set()
        row=len(matrix)
        column=len(matrix[0])
        for i in range(row):
            for j in range(column):
                if(matrix[i][j]==0):
                    zero_set.add((i,j))
        for i,j in zero_set:
            for jj in range(column):
                matrix[i][jj]=0
            for ii in range(row):
                matrix[ii][j]=0
        

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Traceback (most recent call last): File "ADF.py", line 18, in <module> atoms.set_calculator(SinglePointCalculator(atoms, energy=0, forces=np.zeros((len(atoms), 3)), stress=np.zeros(6), neighborlist=nl)) File "/export/home/anliying/.local/lib/python3.8/site-packages/ase-3.22.1-py3.8.egg/ase/calculators/singlepoint.py", line 22, in init assert property in all_properties AssertionError。import numpy as np from ase.io import read from ase.build import make_supercell from ase.visualize import view from ase.neighborlist import NeighborList from ase.calculators.singlepoint import SinglePointCalculator # 读入三个POSCAR文件,计算原子分布函数 structures = [] for file in ['structure1.cif', 'structure2.cif', 'structure3.cif']: atoms = read(file) # 构造超胞,避免周期性边界对ADF计算的影响 atoms = make_supercell(atoms, [[2, 0, 0], [0, 2, 0], [0, 0, 2]]) # 计算邻居列表 nl = NeighborList([1.2] * len(atoms), self_interaction=False) nl.update(atoms) # 将邻居列表传递给SinglePointCalculator atoms.set_calculator(SinglePointCalculator(atoms, energy=0, forces=np.zeros((len(atoms), 3)), stress=np.zeros(6), neighborlist=nl)) # 计算原子分布函数 adf = atoms.get_atomic_distribution_function() structures.append(adf) # 将ADF转化为特征矩阵 bins = np.linspace(0, 10, num=100) # 分100个bin adf_hists = [np.histogram(adf, bins=bins)[0] for adf in structures] feature_matrix = np.array(adf_hists) / [len(atoms) for atoms in structures] # 归一化特征矩阵 feature_matrix = feature_matrix / np.linalg.norm(feature_matrix, axis=1, keepdims=True),基于错误改代码
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值