【发布时间】:2021-07-31 02:46:46
【问题描述】:
我为 HackerRank problem 编写了以下代码,其中涉及语法非常相似的重复 FOR 循环:
x, y = map(int, input().split())
inp = []
for _ in range(x):
inp.append(list(map(int, input().split())))
#I get the rest of the input as a nested list.
while len(inp) < 7:
inp.append([1, 0])
#Adding dummy values in the list
list = []
for a in inp[0][1:inp[0][0]+1]:
for b in inp[1][1:inp[1][0]+1]:
for c in inp[2][1:inp[2][0] + 1]:
for d in inp[3][1:inp[3][0] + 1]:
for e in inp[4][1:inp[4][0] + 1]:
for f in inp[5][1:inp[5][0] + 1]:
for g in inp[6][1:inp[6][0] + 1]:
list.append((a ** 2 + b ** 2 + c ** 2 + d ** 2 + e ** 2 + f ** 2 + g ** 2) % y)
#Given function
print(max(list))
我想知道有没有办法一次性做到这一点。
PS:我绝对是编程初学者。
【问题讨论】:
-
请注意,此问题在站点的
itertools部分。也许可能有一个 itertool 方法可以帮助您找到集合的交叉product()...
标签: python-3.x loops for-loop nested-loops