Numpy 的 tolist(~)
方法將 Numpy 數組轉換為標準 Python 列表。
注意
list(arr)
和 arr.tolist(~)
之間的區別
這兩種方法都將 Numpy 數組轉換為標準 Python 列表。但是,list(arr)
方法不會遞歸執行轉換,也就是說,如果您的 Numpy 數組是 2D,則隻有外部數組會轉換為列表,而內部 Numpy 數組保持原樣。相反,arr.tolist()
會將外部和內部數組轉換為 Numpy 數組。
請參閱下麵的示例以進行說明。
參數
該方法不帶任何參數。
返回值
標準 Python 列表。
例子
a = np.array([5,6,7])
a.tolist()
[5, 6, 7]
a = np.array([[5,6],[7,8]])
a.tolist()
[[5, 6], [7, 8]]
這與 list(~)
方法形成對比:
a = np.array([[5,6],[7,8]])
list(a)
[array([5, 6]), array([7, 8])]
相關用法
- Python torch.distributed.rpc.rpc_async用法及代碼示例
- Python torch.nn.InstanceNorm3d用法及代碼示例
- Python torchaudio.transforms.Fade用法及代碼示例
- Python torch.special.gammaincc用法及代碼示例
- Python torch.optim.lr_scheduler.ConstantLR用法及代碼示例
- Python torch.normal用法及代碼示例
- Python torchdata.datapipes.iter.Multiplexer用法及代碼示例
- Python torch.nn.quantized.dynamic.LSTM用法及代碼示例
- Python torch.nn.EmbeddingBag用法及代碼示例
- Python torch.nn.Module.register_forward_hook用法及代碼示例
- Python torch.nn.AvgPool2d用法及代碼示例
- Python torch.nn.PixelShuffle用法及代碼示例
- Python torch.Generator.initial_seed用法及代碼示例
- Python torch.resolve_neg用法及代碼示例
- Python torchtext.vocab.Vectors.get_vecs_by_tokens用法及代碼示例
- Python torch.nn.CELU用法及代碼示例
- Python torch.reciprocal用法及代碼示例
- Python torch.nn.Hardsigmoid用法及代碼示例
- Python torch.fft.fft用法及代碼示例
- Python torch.distributed.TCPStore用法及代碼示例
- Python torch.distributed.pipeline.sync.skip.skippable.stash用法及代碼示例
- Python torch.nn.GLU用法及代碼示例
- Python torch.nn.functional.conv1d用法及代碼示例
- Python torch.sin用法及代碼示例
- Python torch.jit.save用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | tolist method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。