Pythonコードの改良


1π𔣤1行
💩 Before
for rate in failureRate:
	answer.append(rate[0])
👍 After
answer = [i[0] for i in failureRate]
+) one line if else statement
print("A") if a > b else print("B")
2count関数
EX)List=[1,2,3,4,5,1,4]のうちの1はいくら、2はいくら(番号N=5)
💩 Before
answer = [[0] for i in range(N)]
for i in list:
	answer[i-1] += 1
👍 After
answer = []
for i in range(1,N+1):
	count = list.count(i)
    answer.append(count)
3▼▼▼1▼1▼2▼2▼2▼
💩 Before
dp = []
for _ in range(n):
    dp.append([0 for _ in range(m)])
for i in range(n):
    for j in range(m):
        dp[i][j] = inputs[m*i+j]
👍 After
dp = []
index = 0
for i in range(n):
    dp.append(inputs[index:index + m])
    index += m