[[[-1 for _ in range(COL)] for _ in range(ROW)] for _ in range(7)]
时间: 2023-09-26 09:09:44 浏览: 31
这段代码是一个三重嵌套的列表推导式,用于创建一个 7 层的 2 维列表,每个元素都是一个长度为 COL,元素值为 -1 的列表,每个列表又有 ROW 个元素。
具体操作如下:
1. 外层的 `for` 循环用于循环 7 次,创建 7 层 2 维列表。
2. 中间的 `for` 循环用于循环 ROW 次,创建 ROW 行长度为 COL 的列表。
3. 内层的 `for` 循环用于循环 COL 次,将每个元素初始化为 -1。
4. 列表推导式的结果是一个 7 层的 2 维列表,每个元素都是一个长度为 COL,元素值为 -1 的列表。
因此,这段代码可以用于创建一个 7 层的 2 维列表,并将每个元素初始化为长度为 COL,元素值为 -1 的列表。
相关问题
import numpy as np import pandas as pd import openpyxl from openpyxl.styles import numbers df_csv = pd.read_csv(r'Permance_a.csv', index_col=0, encoding='utf-8') df_csv.to_excel(r'Permance_a.xlsx') # 源表格和目标表格的文件名 source_file = u'Permance_a.xlsx' target_file = u'Permance2.xlsx' # 要复制的单元格位置 source_start_row = 2 # 开始行 source_start_col = 5 # 开始列 source_end_row = 11 # 结束行 source_end_col = 8 # 结束列 # 要覆盖的单元格位置 target_start_row = 7 # 开始行 target_start_col = 3 # 开始列 target_end_row = 16 # 结束行 target_end_col = 6 # 结束列 # 打开两个工作簿 wb1 = openpyxl.load_workbook(source_file) ws1 = wb1.active wb2 = openpyxl.load_workbook(target_file) ws2 = wb2.active # 将源表格的数据覆盖到目标表格中 for i in range(source_start_row, source_end_row + 1): for j in range(source_start_col, source_end_col + 1): cell1 = ws1.cell(row=i, column=j) cell2 = ws2.cell(row=i + target_start_row - source_start_row, column=j + target_start_col - source_start_col) cell2.value = cell1.value # 保存目标表格 wb2.save(target_file) 生成目标文件的单元格显示:此单元格中的数字为文本格式,或者其前面有撇号,修改以上代码解决该问题
可以在将数据覆盖到目标表格之前,先将目标表格中相关单元格的格式设置为数字格式,可以使用openpyxl.styles中的numbers模块来实现。
修改后的代码如下:
```
import numpy as np
import pandas as pd
import openpyxl
from openpyxl.styles import numbers
df_csv = pd.read_csv(r'Permance_a.csv', index_col=0, encoding='utf-8')
df_csv.to_excel(r'Permance_a.xlsx')
# 源表格和目标表格的文件名
source_file = u'Permance_a.xlsx'
target_file = u'Permance2.xlsx'
# 要复制的单元格位置
source_start_row = 2 # 开始行
source_start_col = 5 # 开始列
source_end_row = 11 # 结束行
source_end_col = 8 # 结束列
# 要覆盖的单元格位置
target_start_row = 7 # 开始行
target_start_col = 3 # 开始列
target_end_row = 16 # 结束行
target_end_col = 6 # 结束列
# 打开两个工作簿
wb1 = openpyxl.load_workbook(source_file)
ws1 = wb1.active
wb2 = openpyxl.load_workbook(target_file)
ws2 = wb2.active
# 将目标表格中相关单元格的格式设置为数字格式
for i in range(target_start_row, target_end_row + 1):
for j in range(target_start_col, target_end_col + 1):
cell = ws2.cell(row=i, column=j)
cell.number_format = numbers.FORMAT_NUMBER
# 将源表格的数据覆盖到目标表格中
for i in range(source_start_row, source_end_row + 1):
for j in range(source_start_col, source_end_col + 1):
cell1 = ws1.cell(row=i, column=j)
cell2 = ws2.cell(row=i + target_start_row - source_start_row, column=j + target_start_col - source_start_col)
cell2.value = cell1.value
# 保存目标表格
wb2.save(target_file)
```
这样就可以保证生成的目标文件中的单元格都是数字格式了。
col for row in vec
这是一个Python中的列表推导式(List comprehension),将一个二维列表(或矩阵)进行转置操作,即将每行的元素提取出来,组成新的列。其中的vec是一个二维列表,col表示每列的元素,row表示每行的元素。具体来说,这段代码相当于下面的转置操作:
```
col = []
for row in vec:
for i in range(len(row)):
if len(col) < len(row):
col.append([])
col[i].append(row[i])
```
例如,对于以下二维列表:
```
vec = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
```
进行转置操作后,得到的新二维列表为:
```
[[1, 4, 7],
[2, 5, 8],
[3, 6, 9]]
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)