python查看tuple的形状
时间: 2023-07-24 19:32:48 浏览: 160
Python 源码阅读—tuple
tuple是一种不可变的有序序列,因此它的形状是由其中元素的个数和类型所决定的。你可以使用内置函数`len()`来查看tuple中元素的个数,例如:
```python
t = (1, 2, 3, 4, 5)
print("Tuple t has length:", len(t))
```
输出结果为:
```
Tuple t has length: 5
```
如果你想查看tuple中元素的类型,可以使用内置函数`type()`和Python的切片操作,例如:
```python
t = (1, 2, "three", [4, 5])
for i in range(len(t)):
print("The type of element", i, "is", type(t[i]))
```
输出结果为:
```
The type of element 0 is <class 'int'>
The type of element 1 is <class 'int'>
The type of element 2 is <class 'str'>
The type of element 3 is <class 'list'>
```
阅读全文