Python Numpy数组有头尾方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54269647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:27:46  来源:igfitidea点击:

Is there a head and tail method for Numpy array?

pythonnumpy

提问by ling

I loaded a csv file into 'dataset' and tried to execute dataset.head(), but it reports an error. How to check the head or tail of a numpy array? without specifying specific lines?

我将一个 csv 文件加载到“数据集”中并尝试执行 dataset.head(),但它报告了一个错误。如何检查numpy数组的头部或尾部?没有指定特定的行?

回答by feedMe

For a head-like function you can just slice the array using dataset[:10].

对于类似头部的函数,您可以使用dataset[:10].

For a tail-like function you can just slice the array using dataset[-10:].

对于类似尾部的函数,您可以使用dataset[-10:].

回答by Oliver Dudgeon

You can do this for any python iterable.

您可以对任何可迭代的 Python 执行此操作。

PEP-3132 which is in python 3.x (https://www.python.org/dev/peps/pep-3132/) can use the *symbol for the 'rest' of the iterable.

Python 3.x ( https://www.python.org/dev/peps/pep-3132/) 中的PEP-3132可以使用该*符号表示可迭代对象的“其余部分”。

To do what you want:

做你想做的事:

>>> import numpy as np
>>> np.array((1,2,3))
array([1, 2, 3])
>>> head, *tail = np.array((1,2,3))
>>> head
1
>>> tail
[2, 3]