python查看list的shape
时间: 2023-04-27 20:03:23 浏览: 1434
可以使用内置的"len()"函数查看list的长度, 也可以使用numpy库中的"np.shape"函数查看list的形状。示例代码:
```
import numpy as np
my_list = [1, 2, 3, 4, 5]
print(len(my_list))
print(np.shape(my_list))
```
或者使用python自带的内置函数:
```
my_list = [1, 2, 3, 4, 5]
print(len(my_list))
```
相关问题
python 输出list的shape
在 Python 中,列表(list)是一种序列类型的数据结构,它不像 Numpy 数组那样具有 shape 属性。如果你想输出列表的长度,可以使用 Python 内置函数 len(),例如:
```python
my_list = [1, 2, 3, 4, 5]
print(len(my_list))
```
输出:
```
5
```
如果你想输出列表中每个元素的个数,可以使用列表推导式和 len() 函数,例如:
```python
my_list = [[1, 2], [3, 4, 5], [6]]
shape = tuple(len(row) for row in my_list)
print(shape)
```
输出:
```
(2, 3, 1)
```
这里使用了 tuple() 函数将列表转换为元组,以便输出形状。
python查看list维度
可以使用`numpy`库中的`shape`属性来查看`list`的维度。具体操作如下:
1. 导入numpy库:`import numpy as np`
2. 将`list`转换成`numpy`数组:`arr = np.array(your_list)`
3. 查看数组的维度:`arr.shape`
示例代码:
```
import numpy as np
your_list = [[1,2,3],[4,5,6],[7,8,9]]
arr = np.array(your_list)
print(arr.shape)
```
输出结果为:`(3, 3)`,表示二维数组,第一维有3个元素,第二维有3个元素。