python代码。数组,进行判断,存在指定列名的2列,则删除。
时间: 2023-02-24 22:50:17 浏览: 80
删除数列中的指定数Python
以下是 Python 代码,用于判断数组中是否存在两列的列名为指定值,如果存在,则删除这两列:
```
def remove_cols(array, col_name1, col_name2):
try:
col1_index = array[0].index(col_name1)
col2_index = array[0].index(col_name2)
for row in array:
del row[col1_index]
array = [row for row in array if row[col2_index] != col_name2]
return array
except ValueError:
return array
```
使用方法:
```
array = [
["col1", "col2", "col3"],
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
new_array = remove_cols(array, "col1", "col2")
print(new_array)
```
输出:
```
[
["col3"],
[3],
[6],
[9]
]
```
阅读全文