adict = {} for i in range(100000): adict[str(i)] = i这是什么意思 翻译一下
时间: 2024-04-26 09:20:53 浏览: 19
这段代码的意思是:创建一个空字典 `adict`,然后循环 100000 次,每次循环都给字典 `adict` 添加一个键为字符串类型的数字(从 0 到 99999)对应的值为相应的整数。具体来说,`adict[str(i)] = i` 表示将整数 `i` 转换为字符串类型后,将其作为字典 `adict` 的键,对应的值为整数 `i`。这样就创建了一个包含 100000 个键值对的字典。
相关问题
ls1 = {} ls2 = [] ls3 = [['班级', '学号', '姓名', '测试1(百分制)', '测试2(百分制)', '测试3(百分制)', '测试4(百分制)', '总分', '备注']] with open('total.csv', mode='r') as f: r = f.readlines() n = 0 for l in r: ls4 = l.split(',') ls4[-1] = ls4[-1][:-1] if n == 0: for t in ls4[3:]: num = '' for s in t: if s.isdigit(): num += s if s == '.': break ls2.append(int(num)) else: lens = len(ls4[3:]) for j in range(lens): if ls4[3 + j] == '缺考': ls4[3 + j] = 0 ls1[ls4[2]] = list(map(int, [ls4[3], ls4[4], ls4[5], ls4[6]])) n += 1 for t, v in ls1.items(): f = len(v) for k in range(f): v[k] = (v[k] / ls2[k]) * 100 // 1 p = sum(v) v.append(p) with open('total.csv', mode='r') as f: n = f.readlines() for l in r: ls = l.split(',') ls[-1] = ls[-1][:-1] ls3.append(ls[:3]) a = 1 for t, v in ls1.items(): for o in v: o = str(o) ls3[a + 1].append(o) a += 1 ls3.pop(1) with open('最终成绩单.csv', mode='w') as f: for i in ls3: s = ','.join(i) s += '\n' f.write(s) print(i) 改一下变量
好的,我来帮您改一下变量名:
```python
dict_scores = {}
list_max_scores = []
list_table_header = [['班级', '学号', '姓名', '测试1(百分制)', '测试2(百分制)', '测试3(百分制)', '测试4(百分制)', '总分', '备注']]
with open('total.csv', mode='r') as f:
r = f.readlines()
n = 0
for l in r:
list_line = l.split(',')
list_line[-1] = list_line[-1][:-1]
if n == 0:
for t in list_line[3:]:
num = ''
for s in t:
if s.isdigit():
num += s
if s == '.':
break
list_max_scores.append(int(num))
else:
len_scores = len(list_line[3:])
for j in range(len_scores):
if list_line[3 + j] == '缺考':
list_line[3 + j] = 0
dict_scores[list_line[2]] = list(map(int, [list_line[3], list_line[4], list_line[5], list_line[6]]))
n += 1
for t, v in dict_scores.items():
len_scores = len(v)
for k in range(len_scores):
v[k] = (v[k] / list_max_scores[k]) * 100 // 1
total_score = sum(v)
v.append(total_score)
with open('total.csv', mode='r') as f:
n = f.readlines()
for l in r:
list_line = l.split(',')
list_line[-1] = list_line[-1][:-1]
list_table_header.append(list_line[:3])
row_num = 1
for t, v in dict_scores.items():
for score in v:
score = str(score)
list_table_header[row_num + 1].append(score)
row_num += 1
list_table_header.pop(1)
with open('最终成绩单.csv', mode='w') as f:
for i in list_table_header:
line = ','.join(i)
line += '\n'
f.write(line)
print(i)
```
TypeError: Value must be a list, tuple, range or generator, or a dict. Supplied value is <class 'str'>
这个错误通常是由于你在使用 openpyxl 的 API 时,传入了错误的数据类型。比如,如果你使用 `append` 方法来向工作表中添加一行数据,那么你需要传入一个列表(list)类型的数据,而不是一个字符串类型的数据。
你可以检查一下你的数据是否满足要求。如果你需要添加一行数据,那么你应该使用一个列表来表示这一行数据,比如:
```
row_data = ['a', 'b', 'c']
worksheet.append(row_data)
```
如果你想要添加多行数据,那么你可以将这些行数据放入一个列表中,比如:
```
data = [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i']
]
for row_data in data:
worksheet.append(row_data)
```
如果你还有其他问题,可以提供更多上下文信息,让我能够更好地理解你的问题。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)