Python 对列表中的每个元素进行配对
我有一个整数列表和一个输入Python 对列表中的每个元素进行配对,python,python-2.7,Python,Python 2.7,我有一个整数列表和一个输入x。我必须找出我的列表中是否有两个数字,它们的平方和等于x 我使用以下方法完成了此操作: def findsquare(n1, n2, inp): result = n1*n1 + n2*n2 if result == inp: return True a = [2, -4, 6, 3, 9, 0 , -1, -9] x = 45 i = 0 while i < (len(a)-1): res = findsquar
x
。我必须找出我的列表中是否有两个数字,它们的平方和等于x
我使用以下方法完成了此操作:
def findsquare(n1, n2, inp):
result = n1*n1 + n2*n2
if result == inp:
return True
a = [2, -4, 6, 3, 9, 0 , -1, -9]
x = 45
i = 0
while i < (len(a)-1):
res = findsquare(a[i], a[i+1], x)
if res:
print "Match: " + str(a[i]) + ", " + str(a[i+1])
else:
print "No Match: " + str(a[i]) + ", " + str(a[i+1])
i = i+1
def findsquare(n1、n2、inp):
结果=n1*n1+n2*n2
如果结果==inp:
返回真值
a=[2,-4,6,3,9,0,-1,-9]
x=45
i=0
而我<(len(a)-1):
res=findsquare(a[i],a[i+1],x)
如果有的话:
打印“匹配:“+str(a[i])+”,“+str(a[i+1]”)
其他:
打印“不匹配:“+str(a[i])+”,“+str(a[i+1]”)
i=i+1
这个元素的问题在于,它只能与下一个元素进行比较。例如:
2和-4进行比较,-4和6进行比较。我想做的是将2与列表中的每个元素进行比较,将-4与列表中的每个其他元素进行比较,依此类推。我希望在不使用python中内置函数的情况下执行此操作 您只需要跟踪两个索引,因为您要多次查看列表。如果i
跟踪比较中的第一个元素,则需要另一个j
跟踪比较中的第二个元素。现在j
可以是列表的任何其他索引,但是如果您只想进行一次比较,可以从i+1
开始:
for i in range(len(a)):
for j in range(i + 1, len(a)):
# compare the elements here
print(a[i], a[j])
您只需要跟踪两个索引,因为您要多次浏览列表。如果i
跟踪比较中的第一个元素,则需要另一个j
跟踪比较中的第二个元素。现在j
可以是列表的任何其他索引,但是如果您只想进行一次比较,可以从i+1
开始:
for i in range(len(a)):
for j in range(i + 1, len(a)):
# compare the elements here
print(a[i], a[j])
用于生成所有对,例如
import itertools
a = [2, -4, 6, 3, 9, 0 , -1, -9]
for i in itertools.combinations(a, 2):
print i
输出:
(2, -4)
(2, 6)
(2, 3)
(2, 9)
(2, 0)
(2, -1)
(2, -9)
(-4, 6)
(-4, 3)
(-4, 9)
(-4, 0)
(-4, -1)
(-4, -9)
...
itertools.composition相当于:
def combinations(iterable, r):
# combinations('ABCD', 2) --> AB AC AD BC BD CD
# combinations(range(4), 3) --> 012 013 023 123
pool = tuple(iterable)
n = len(pool)
if r > n:
return
indices = range(r)
yield tuple(pool[i] for i in indices)
while True:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range(i+1, r):
indices[j] = indices[j-1] + 1
yield tuple(pool[i] for i in indices)
用于生成所有对,例如
import itertools
a = [2, -4, 6, 3, 9, 0 , -1, -9]
for i in itertools.combinations(a, 2):
print i
输出:
(2, -4)
(2, 6)
(2, 3)
(2, 9)
(2, 0)
(2, -1)
(2, -9)
(-4, 6)
(-4, 3)
(-4, 9)
(-4, 0)
(-4, -1)
(-4, -9)
...
itertools.composition相当于:
def combinations(iterable, r):
# combinations('ABCD', 2) --> AB AC AD BC BD CD
# combinations(range(4), 3) --> 012 013 023 123
pool = tuple(iterable)
n = len(pool)
if r > n:
return
indices = range(r)
yield tuple(pool[i] for i in indices)
while True:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range(i+1, r):
indices[j] = indices[j-1] + 1
yield tuple(pool[i] for i in indices)
当我看到你的问题时,我碰巧正在使用这些:
def choose_two (L):
for i in xrange(len(L)):
for ii in xrange(i+1,len(L)):
yield (L[i],L[ii])
def xprod (L1,L2):
for l in L1:
for l2 in L2:
yield (l,l2)
后者是叉积,而前者产生所有可能的对。所以你会这样做:
a = [2, -4, 6, 3, 9, 0 , -1, -9]
x = 45
is_in = x in [a1**2+a2**2 for (a1,a2) in choose_two(a)]
当我看到你的问题时,我碰巧正在使用这些:
def choose_two (L):
for i in xrange(len(L)):
for ii in xrange(i+1,len(L)):
yield (L[i],L[ii])
def xprod (L1,L2):
for l in L1:
for l2 in L2:
yield (l,l2)
后者是叉积,而前者产生所有可能的对。所以你会这样做:
a = [2, -4, 6, 3, 9, 0 , -1, -9]
x = 45
is_in = x in [a1**2+a2**2 for (a1,a2) in choose_two(a)]