N = int(input("Write a number between 3 and 10 to the power of 18: "))
P = int(input("How many questions did Partick get right: "))
a,b = [],[]
for i in range(P):
Pi = int(input("Write the indexes of the questions: "))
a.append(Pi)
F = int(input("How many questions did Fabijan get right: "))
for i in range(F):
Fi = int(input("Write the indexes of the questions: "))
b.append(Fi)
g = 0
for i in range(len(a)):
在这里,我遇到了一个障碍。我想将列表a
的所有元素与列表b
的所有元素进行比较。为了更清楚起见,我想将列表a
的所有元素与列表b
的每个元素进行比较,而不知道列表b
的确切长度。
您想在b
中计算每次出现的a[i]
,还是只计算一次(如果发生(?
如果是后者,那么您的解决方案可以在另一个答案和评论中找到。
为了完整起见,我会回答假设你的意思是前一个问题。
遍历a
,然后遍历b
,并在每次元素之间匹配时计数。
g = 0
for i in a:
for j in b:
if i == j:
g += 1
此外,这可以使用list.count
和sum
来简化
g = sum(b.count(i) for i in a)
你可以只使用"in"。像这样:
for i in range(len(a)):
if a[i] in b: