python 数组为空判断
时间: 2024-07-20 12:01:30 浏览: 129
Array-Editor
在Python中,数组(列表、元组或数组等)为空的判断通常通过检查它的长度是否为0来进行。以下是几种常见情况下的判断:
1. 列表为空:
```python
my_list = []
if not my_list:
print("List is empty")
```
2. 元组为空:
```python
my_tuple = ()
if not my_tuple:
print("Tuple is empty")
```
3. NumPy数组(如果使用NumPy库)为空:
```python
import numpy as np
empty_array = np.array([])
if empty_array.size == 0:
print("Array is empty")
```
4. Pandas DataFrame或Series为空:
```python
import pandas as pd
empty_df = pd.DataFrame()
if empty_df.empty:
print("DataFrame is empty")
```
阅读全文