如何用python解决以下问题

有四个整数序列。每个整数取值范围为-50,000,000 ~ 50,000,000(含50,000,000)。编写一个Python 3程序,查找一组由四个数字组成的集合,每个序列都有一个数字,其和为零。
对于任何测试用例,运行时间不能超过1秒。

输入:四行,每行为n个整数序列,n≤1000,用空格隔开。

输出:4个数字,来自4个不同的序列,和为0,或者“不存在”如果这4个数字不存在。如果有多于一组四个数之和为零,则输出任意一组。

比如

img

参考如下代码,可以运行出希望的结果,至于运行时间会不会超时,你测试下看看:

import numpy as np
import random
a = np.array([[int(x) for x in input().split()] for _ in range(4)])
if any(a.sum(axis=0) == 0):
    s = random.choice(a[:, a.sum(axis=0) == 0].T)
else:
    s=[0]*a.shape[1]
print(*s)

运行结果:

#1
1 2 3 4   
2 1 3 5
0 2 1 4
-3 -5 2 1
1 2 0 -3
#2
0 1 2 3 
2 3 4 5
1 2 3 4 
4 5 6 7 
0 0 0 0

按补充解释要求,用product来所有行元素的组合,用如下代码即可:

from itertools import product
import random
a = [[int(x) for x in input().split()] for _ in range(4)]
b=[x for x in product(*a) if sum(x)==0]
if len(b)!=0:
    print(*random.choice(b))
else:
    print('不存在')

运行结果:
-1121284 2077198 -3214647 2258733