python - 如何在二维数组/列表的相应索引中附加文本文件的变量值?
问题描述
代码:
opener = open("gymclub.txt", "r")
reader = opener.readline()
listPressups = [["",],["",],["",],["",],["",],["",],["",],["",],["",],["",],["",],["",],["",]]
while reader!="":
splitting=reader.split(",")
name = splitting[0]
press_ups = splitting[1]
pull_ups = splitting[2]
reader = opener.readline()
for x in range(1,12):
listPressups[0][x].append(int(press_ups))
listPressups.sort(reverse=True)
print(listPressups)
输出:
Traceback (most recent call last):
File "C:/Users/Nutzer/Desktop/Python/practice_NEA/index.py", line 36, in <module>
listPressups[0][x].append(int(press_ups))
IndexError: list index out of range
期望的输出:
[["",75],["",74],["",73],["",67],["",66],["",58],["",45],["",33],["",30],["",25],["",10],["",8]]
我可以使用什么方法来达到我想要的输出?
我使用的文本文件:
解决方案
尝试这个:
opener = open("gymclub.txt", "r")
listPressups = []
for line in opener.readlines():
press_ups = int(line.split(",")[1])
listPressups.append(["", press_ups])
listPressups.sort(reverse=True)
opener.close()
print(listPressups)
推荐阅读
- python - Selenium Python 无法提取 [object Text] 它应该是一个元素
- javascript - 将日期转换为 M j, Y?在 Javascript 中
- vue.js - 我想要做的是使用单个打印按钮从多台打印机同时打印表单的不同打印输出
- regex - 带有组名并包含标签的正则表达式 XML
- sql - 基于字符串模式检索父记录的 Oracle SQL 查询
- cryptography - 密码学:关于流密码的问题
- python - 如何为循环的特定部分添加延迟
- javascript - 将组件作为 React setState 值传递时有问题吗?
- python - sqlite3 向表中添加行不添加任何错误
- c# - 如何提高选项卡式页面 xamarin 表单中的加载性能