手动模拟一下多项式乘法的操作就知道如何做了
from collections import defaultdict
a = {}
b = {}
product = defaultdict(float)
li = list(map(float,input().split()))
for i in range(1,len(li),2):
a[int(li[i])] = li[i + 1]
li = list(map(float,input().split()))
for i in range(1,len(li),2):
b[int(li[i])] = li[i + 1]
for k1,v1 in a.items():
for k2,v2 in b.items():
e = k1 + k2
c = v1 * v2
product[e] += c
cnt = 0
product = list(product.items())
product.sort(key = lambda item : -item[0])
for k,v in product:
if v != 0:
cnt += 1
print(cnt,end='')
for k,v in product:
if v != 0:
print(" %d %.1f" % (k,v),end = '')# 也可以使用格式化字符串的方式:print(f" {k} {v:.1f}", end='')或print(" {} {:.1f}".format(k, v), end='')