简体   繁体   English

Python中list [index,:]的含义是什么

[英]what's the meaning of list[index, :] in Python

What's the difference between list[index, :] and list[index:]?? list [index,:]和list [index:]有什么区别?

for line in arraylines:
   line  = line.strip()
   listFromLine = line.split('\t')
   returnMat[index, :] = listFromLine[0:3]

At the beginning, returnMat is a zero matrix. 刚开始,returnMat是零矩阵。

returnMat[index, :] is an example of numpy's array slicing syntax. returnMat[index, :]是numpy的数组切片语法的示例。 It would retrieve rows (different rows correspond to the first index) of the matrix ( : means "get all of this index"). 它将检索矩阵的行(不同的行对应于第一个索引)( :表示“获取所有该索引”)。 Example: 例:

import numpy as np

mat = np.zeros((3, 5))

print(mat)  # 3 rows and 5 columns of zeros
mat[1, :] = 1
print(mat)  # all of second row is now ones

To go a bit further, it looks like you're constructing a matrix row-by-row from a file in your code snippet, which means you probably need to be doing something with index as well. 更进一步,看起来您似乎是从代码段中的文件逐行构造矩阵,这意味着您可能还需要对index进行处理。 I would suggest looking into enumerate . 我建议寻找到enumerate

list[index,:]: starts from index number till the end
list[index:]: it has ranged between numbers which are the output.

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

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