[英]issue with comparing elements within tuples of a list in python3
I am trying to compare elements within the tuple of a list and extract only the values which are not common but for some reason it is returning the whole combination. 我试图比较列表的元组中的元素,并仅提取不常见的值,但由于某种原因,它返回整个组合。 For example if I pass the values like this:
例如,如果我传递这样的值:
[(3,2), (2,4)]
then it should return 然后它应该返回
[(3,4)]
This is what I tried: 这是我尝试的:
a=[]
for i in range(len(l)):
j=i+1
for j in range(len(l)):
print(i)
print(j)
if l[i][0]==l[j][0]:
a.append((l[i][1],l[j][1]))
print(a)
elif l[i][0]==l[j][1]:
a.append((l[i][1],l[j][0]))
print(a)
elif l[i][1]==l[j][0]:
a.append((l[i][0],l[j][1]))
print(a)
elif l[i][1]==l[j][1]:
a.append((l[i][0],l[j][0]))
print(a)
I was trying to build a more generic code to handle different kinds of inputs but it's not able to handle all the cases. 我试图构建一个更通用的代码来处理不同种类的输入,但是它不能处理所有情况。 Its giving both the possibilities for a single comparison for eg [(3,2),(2,4)].It gives both 3 4 and 4 3 as output.
它为[[3,2),(2,4)]的单个比较提供了两种可能性。它给出3 4和4 3均为输出。
Sample inputs tried
>>onehop([(2,3),(1,2)]) input
>>[(1, 3)] expected output
>>onehop([(2,3),(1,2),(3,1),(1,3),(3,2),(2,4),(4,1)]) input
>>[(1, 2), (1, 3), (1, 4), (2, 1), (3, 2), (3, 4), (4, 2), (4, 3)] output
>>onehop([(1,2),(3,4),(5,6)]) input
>>[] expected output
>>onehop([(1,2),(2,1)]) input
>>[ ] expected output
>>onehop([(1,2)]) input
>>[ ] expected output
>>onehop([(1,3),(1,2),(2,3),(2,1),(3,2),(3,1)]) input
>>[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)] expected output
Is there a more optimized code or a list comprehension possible. 是否有更优化的代码或可能的列表理解。 I am new to this and learning this This is what I have tried.
我对此并不陌生,正在学习这是我尝试过的方法。
def onehop(l):
a = []
b = []
c = []
for i in range(len(l)):
j = i+1
for j in range(len(l)):
print(i) 'Just to understand the loops'
print(j)
if l[i][0] == l[j][1] and l[i][1] != l[j][0]:
a.append((l[i][1],l[j][0]))
elif l[i][0] != l[j][1] and l[i][1] == l[j][0]:
a.append((l[i][0],l[j][1]))
elif l[i][0] == l[j][0] and l[i][1] != l[j][1]:
a.append((l[i][1],l[j][1]))
elif l[i][0] != l[j][0] and l[i][1] == l[j][1]:
a.append((l[i][0],l[j][0]))
b = list(set(a))
b.sort()
return b
It seems like an oversight but you set j
and immediatly overwrite it with the for
-loop, which explains why you get all combinations: It also compares each element with itself. 看起来像是一个疏忽,但是您设置了
j
并立即用for
-loop覆盖了它,这说明了获得所有组合的原因:它还将每个元素与其自身进行比较。
If you insert what your manually calculated j
as lower bound it seems to work for your case: 如果插入手动计算的
j
作为下限,则似乎适用于您的情况:
l = [(3,2),(2,4)]
a = []
for i in range(len(l)):
for j in range(i+1, len(l)): # changed!
if l[i][0]==l[j][0]:
a.append((l[i][1],l[j][1]))
elif l[i][0]==l[j][1]:
a.append((l[i][1],l[j][0]))
elif l[i][1]==l[j][0]:
a.append((l[i][0],l[j][1]))
elif l[i][1]==l[j][1]:
a.append((l[i][0],l[j][0]))
print(a)
# [(3, 4)]
Probably you could simplify the code (and make it faster) by using set
operations like as @Bill Bell pointed out already: 可能您可以使用@Bill Bell指出的
set
操作来简化代码(并使其更快):
For instance,
tuple(set((3,2)).symmetric_difference(set((2,4))))
yields(3, 4)
例如,
tuple(set((3,2)).symmetric_difference(set((2,4))))
得出(3, 4)
def remove_dup(L):
if L == ():
return ()
else:
return L[0] + remove_dup(tuple(map(lambda li: tuple(filter(lambda x: x not in L[0], li)), L[1:])))
print(remove_dup([(3,2),(2,4)]))
-->
(3, 2, 4)
print(remove_dup([(3,2,9),(2,4),(4,3,8,9),(9,7),(8,)]))
-->
(3, 2, 9, 4, 8, 7)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.