python提取第一列
时间: 2023-09-23 22:02:34 浏览: 108
假设你有一个名为data的二维数组(或者叫矩阵),你可以使用以下代码提取第一列:
```
first_column = [row[0] for row in data]
```
这行代码使用列表推导式,首先遍历data的每一行,然后获取每一行的第一个元素(即第一列),将所有第一个元素组成的列表赋值给变量first_column。
相关问题
python提取第一列数据代码
你可以使用Python的csv模块来提取CSV文件的第一列数据。以下是一个示例代码:
```python
import csv
with open('your_csv_file.csv', 'r') as file:
reader = csv.reader(file)
column1 = []
for row in reader:
column1.append(row[0])
print(column1)
```
你需要将`your_csv_file.csv`替换为你实际要读取的CSV文件的路径。运行代码后,它会将第一列数据存储在一个名为`column1`的列表中,并将其打印出来。
python提取第二列
假设你有一个二维列表 `lst`,可以使用以下代码来提取第二列:
```python
second_col = [row[1] for row in lst]
```
其中 `row[1]` 表示每个子列表的第二个元素,`[row[1] for row in lst]` 表示将每个子列表的第二个元素放入一个新的列表中。
阅读全文
相关推荐
















