优化提升下列代码效率:class User: def init(self): self.id = 0 self.perfect = 0 self.rank = 0 self.mk = -1 self.grade = [0, -1, -1, -1, -1, -1] def lt(self, other): if self.grade[0] != other.grade[0]: return self.grade[0] > other.grade[0] elif self.perfect != other.perfect: return self.perfect > other.perfect else: return self.id < other.id if name == "main": N, K, M = map(int, input().split()) p = [0] + list(map(int, input().split())) u = [None] * (N + 1) idx = [] for i in range(M): tmp, pid, tmpgrade = map(int, input().split()) if not u[tmp]: u[tmp] = User() u[tmp].id = tmp if tmpgrade != -1: u[tmp].mk = 1 else: tmpgrade = 0 if tmpgrade > u[tmp].grade[pid]: u[tmp].grade[pid] = tmpgrade if not idx.contains(tmp): idx.append(tmp) for i in range(len(idx)): for j in range(1, K + 1): if u[idx[i]].grade[j] >= 0: u[idx[i]].grade[0] += u[idx[i]].grade[j] if u[idx[i]].grade[j] == p[j]: u[idx[i]].perfect += 1 al = sorted([u[i] for i in idx if u[i].mk > 0]) for i in range(len(al)): if i == 0: al[i].rank = 1 else: if al[i].grade[0] == al[i - 1].grade[0]: al[i].rank = al[i - 1].rank else: al[i].rank = i + 1 for i in range(len(al)): print("{} {:05d}".format(al[i].rank, al[i].id), end="") for j in range(K + 1): if al[i].grade[j] >= 0: print(" {:d}".format(al[i].grade[j]), end="") else: print(" -", end="") if i != len(al) - 1: print()
时间: 2024-01-04 09:02:16 浏览: 139
高效率、简洁、CSS代码优化原则.docx
首先,可以使用 set() 代替 idx 列表,因为我们只需要判断一个元素是否在集合中。这样可以减少重复的判断。
其次,在 User 类的初始化函数中,可以直接将属性设置为默认值,而不是在每个属性上进行赋值。
另外,在循环中,可以使用 enumerate() 函数代替 range() 函数和下标变量,这样可以避免手动维护下标变量。
最后,在计算排名时,可以使用 itertools.groupby() 函数实现,这样可以避免手动维护排名和比较。
下面是优化后的代码:
```python
import itertools
class User:
def __init__(self):
self.id = 0
self.perfect = 0
self.rank = 0
self.mk = -1
self.grade = [0, -1, -1, -1, -1, -1]
def lt(self, other):
if self.grade[0] != other.grade[0]:
return self.grade[0] > other.grade[0]
elif self.perfect != other.perfect:
return self.perfect > other.perfect
else:
return self.id < other.id
if __name__ == "__main__":
N, K, M = map(int, input().split())
p = [0] + list(map(int, input().split()))
u = [None] * (N + 1)
idx = set()
for i in range(M):
tmp, pid, tmpgrade = map(int, input().split())
if not u[tmp]:
u[tmp] = User()
u[tmp].id = tmp
if tmpgrade != -1:
u[tmp].mk = 1
else:
tmpgrade = 0
if tmpgrade > u[tmp].grade[pid]:
u[tmp].grade[pid] = tmpgrade
idx.add(tmp)
for i, user in enumerate(u):
if user and user.mk > 0:
user.grade[0] = sum(user.grade[j] for j in range(1, K + 1) if user.grade[j] >= 0)
user.perfect = sum(user.grade[j] == p[j] for j in range(1, K + 1))
al = sorted([user for user in u if user and user.mk > 0], key=lambda x: (-x.grade[0], -x.perfect, x.id))
for rank, group in itertools.groupby(al, key=lambda x: (x.grade[0], x.perfect)):
for user in group:
user.rank = rank
for user in sorted(list(group), key=lambda x: x.id):
print("{:d} {:05d}".format(user.rank, user.id), end="")
for j in range(1, K + 1):
if user.grade[j] >= 0:
print(" {:d}".format(user.grade[j]), end="")
else:
print(" -", end="")
print()
```
阅读全文