Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何找到一个数组中的哪些元素与另一个数组中的每个元素相加?_Python_Arrays_List_Debugging_Encryption - Fatal编程技术网

Python 如何找到一个数组中的哪些元素与另一个数组中的每个元素相加?

Python 如何找到一个数组中的哪些元素与另一个数组中的每个元素相加?,python,arrays,list,debugging,encryption,Python,Arrays,List,Debugging,Encryption,假设: reversedPriv = [52,27,13,6,3,2] array= [9] #For this example we only used one element in the array, assume more elements var = 0 numA = [] for i in array: for j in reversedPriv: while var!= j: if j < i:

假设:

reversedPriv = [52,27,13,6,3,2]
array= [9] #For this example we only used one element in the array, assume more elements
var = 0
numA = []
for i in array:
    for j in reversedPriv:
        while var!= j:
            if j < i:
                var = var + j
                numA.append(j)
                numA.sort()
print(numA)
reversedPriv=[52,27,13,6,3,2]
array=[9]#在这个例子中,我们只使用了数组中的一个元素,假设有更多的元素
var=0
numA=[]
对于数组中的i:
对于反向Priv中的j:
而var!=j:
如果j
我希望它能
将[3,6]
附加到
numA
并打印出来,但目前它什么也不做。我忽略的while循环有什么条件吗


代码的要点是找出
reversedPriv
中的哪些元素与
array
中的每个元素相加,并将它们附加到列表
numA
。例如,从
reversedPriv
列表中,只有6和3加起来是9。因此
numA=[3,6]
目前“数组”只有一个元素,但代码应该能够将其放大n个元素。

以下是一种方法:

import itertools 
import pandas as pd
df = pd.DataFrame(itertools.combinations( [52,27,13,6,3,2], 2))  
nums = [9] 
df['sum'] = df[0] + df[1] 
matches=[] 
for num in nums: 
    matches.append(list(df.loc[df['sum'] == num][0]) + list(df.loc[df['sum'] == num][1])) 

matches
# [[6, 3]]  

有什么好处。不要多次发布同一问题。这是否回答了您的问题?