【问题标题】:issue with comparing elements within tuples of a list in python3比较python3中列表的元组中的元素的问题
【发布时间】:2018-01-31 06:42:40
【问题描述】:

我正在尝试比较列表元组中的元素并仅提取不常见但由于某种原因它返回整个组合的值。例如,如果我传递这样的值:

[(3,2), (2,4)]

那么它应该返回

[(3,4)]

这是我尝试过的:

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)

我试图构建一个更通用的代码来处理不同类型的输入,但它无法处理所有情况。它为例如 [(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

是否有更优化的代码或列表理解可能。我是新手,正在学习这个 这是我尝试过的。

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

【问题讨论】:

  • 提示:如果你使用集合,你可以大大简化这段代码。
  • 例如,tuple(set((3,2)).symmetric_difference(set((2,4)))) 产生 (3, 4)

标签: python python-3.x list tuples


【解决方案1】:

这似乎是一个疏忽,但您设置了j 并立即用for-loop 覆盖它,这解释了为什么您会得到所有组合:它还将每个元素与其自身进行比较。

如果您插入手动计算的 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)]

您可能可以通过使用set 操作(如@Bill Bell 已经指出的那样)来简化代码(并使其更快):

例如,tuple(set((3,2)).symmetric_difference(set((2,4)))) 产生 (3, 4)

【讨论】:

  • 我尝试根据输入更多地概括代码。但我认为它没有处理其他场景。
  • 那么请编辑您的问题以包含“其他场景”。
  • @MRaviPrakash 请编辑您的问题。您可以在那里使用标记,这样任何人都可以更轻松地看到您的意图。
  • 感谢我编辑问题的输入,因为我无法在评论中回答我的疑问,希望能有所帮助
【解决方案2】:
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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 2015-09-30
    • 2023-04-03
    相关资源
    最近更新 更多