【问题标题】:'list' object is not callable for find same elements in two different list'list' 对象不可调用以在两个不同的列表中查找相同的元素
【发布时间】:2021-03-09 17:21:48
【问题描述】:

我是Python新手,不知道哪里出错了

def q(listA, listB):

    list1=[]
    for a in range(len(listA)):
        for b in range(len(listB)):
            if listA(a)==listB(b):
                list1.append(a)
    list2=[]
    for x in range(len(listb)):
        for y in range(len(listA)):
            if listB(x)==listA(y):
                list2.append(x)
    
    
    return list(zip(list1,list2))

pairAPy = [1,2,3,4]
pairBPy = [1,5,7,2]
print(q(pairAPy,pairBPy))
Line 21 print(q(pairAPy,pairBPy))  
 Line 8             if listA(a)==listB(b):  
 TypeError: 'list' object is not callable

答案应该返回[(0, 0), (2, 1), (3, 3)]

【问题讨论】:

  • 你想要if listA[a] == listB[b]
  • if listA[a]==listB[b]

标签: python for-loop


【解决方案1】:

您不小心使用了(a)(b) 而不是[a][b]。您的代码应如下所示:

def q(listA, listB):

    list1 = []
    for a in range(len(listA)):
        for b in range(len(listB)):
            **if listA[a] == listB[b]:
                list1.append(a)
    list2=[]
    for x in range(len(listb)):
        for y in range(len(listA)):
            if listB[x] == listA[y]:
                list2.append(x)
    
    
    return list(zip(list1, list2))

pairAPy = [1,2,3,4]
pairBPy = [1,5,7,2]
print(q(pairAPy, pairBPy))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    相关资源
    最近更新 更多