问题描述
我想知道在对 Python 数组进行切片时逗号的用途是什么 - 我有一个似乎可以工作的示例,但对我来说看起来很奇怪的行是
p = 20*numpy.log10(numpy.abs(numpy.fft.rfft(data[:2048, 0])))
现在,我知道对数组进行切片时,第一个数字是开始,下一个是结束,最后一个是步骤,但是结束数字后面的逗号表示什么?谢谢.
它用于从二维数组中提取特定的列.请参阅此处的第一个示例.>
因此,您的示例将从前 2048 行(0 到 2047)中提取第 0 列(第一列).但请注意,此语法仅适用于 numpy 数组,不适用于一般的 Python 列表.
I was wondering what the use of the comma was when slicing Python arrays - I have an example that appears to work, but the line that looks weird to me is
p = 20*numpy.log10(numpy.abs(numpy.fft.rfft(data[:2048, 0])))
Now, I know that when slicing an array, the first number is start, the next is end, and the last is step, but what does the comma after the end number designate? Thanks.
It is being used to extract a specific column from a 2D array. Refer to the first examples here.
So your example would extract column 0 (the first column) from the first 2048 rows (0 to 2047). Note however that this syntax will only work for numpy arrays and not general python lists.
这篇关于带逗号的 Python 数组切片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!