Python 3.x 如何查看两个不同元组列表中的元素是否匹配

Python 3.x 如何查看两个不同元组列表中的元素是否匹配,python-3.x,Python 3.x,我有两个元组列表,它们最终帮助我实现流程自动化。我想看看如何正确识别元组列表中是否有匹配项 我已经尝试将元组列表展平,以使它们更易于使用,我想我想确定元素是否与布尔语句匹配,但我不确定如何让程序运行这两个元组列表 list1 = [[('1306264304', 'Coca-Cola Zero Sugar Cola, Cans', '1'), ('1176982083', "Ito En Teas\\' Tea, Jasmine Green, Plastic Bottles", '1'), ('

我有两个元组列表,它们最终帮助我实现流程自动化。我想看看如何正确识别元组列表中是否有匹配项

我已经尝试将元组列表展平,以使它们更易于使用,我想我想确定元素是否与布尔语句匹配,但我不确定如何让程序运行这两个元组列表

list1 = [[('1306264304', 'Coca-Cola Zero Sugar Cola, Cans', '1'), ('1176982083', "Ito En Teas\\' Tea, Jasmine Green, Plastic Bottles", '1'), ('-975890652', "Ito En Teas\\' Tea, Lemongrass Green, Plastic Bottles", '1'), ('-1152939818', "Ito En Teas\\' Tea, Pure Green, Plastic Bottles", '1'), ('19102859', 'LaCroix Sparkling Water, Coconut, Cans', '1'), ('-546157568', 'LaCroix Sparkling Water, Grapefruit, Cans', '1')]] 

and

list2 = [[((beverages)'Coca-Cola Zero Sugar Cola, Cans', 4), ("Ito En Teas\\' Tea, Jasmine Green, Plastic Bottles", 3), ("Ito En Teas\\' Tea, Pure Green, Plastic Bottles", 5)]]

如果列表1中的元组元素与列表2中的元组元素匹配,我需要它返回true。例如:如果“可口可乐零糖可乐,罐头”出现在两个列表中,我需要让它识别匹配项。实际上,我对如何编写代码有点迷茫。我知道我需要一个循环,但仅此而已。非常感谢你的帮助

这应该能奏效。该函数在中得到了很好的注释,但本质上,我们在一个数组中循环,并对照第二个数组中的项检查每个项。如果存在匹配项,则返回true。如果不是,则返回false

# Create function
def find_match(arr1, arr2):
    # Loop through first array
    for item in arr1:
        # Given the current item, check it against items in other array
        for arr2_item in arr2:
            # Print what you are comparing
            print("array 1 item: ", item, "array 2 item: ", arr2_item)
            # If there is a match
            if item == arr2_item:
                # Return true
                return True
        # If not
        else:
            # Keep going
            continue
    # If you havent returned by this point, it means there is no match
    return False

# Main function
def main():
    # Example dummy list one
    list1 = ['a', 'b', 'c', 'd']
    # Example dummy list two
    list2 = ['d', 'e', 'f', 'g']

    # Call the function
    did_it_match = find_match(list1, list2)
    # Print the result
    print(did_it_match)

# Call main    
main()   
注意-您发布的数组似乎格式不正确,因此我无法使用这些数组

**

**根据OP指定的指南进行更新** ** 下面的代码是从发布的另一个代码示例中编辑的,但它是使用OP发布的数组的有效解决方案

def solution(list1, list2):
    for index, item in enumerate(list1[0]):
        for index, item2 in enumerate(list2[0]):

            for item3 in item:

                for item4 in item2:

                    if item3==item4:
                        return True

    return False

def main():
    list1 = [[('1306264304', 'Coca-Cola Zero Sugar Cola, Cans', '1'), ('1176982083', "Ito En Teas\\' Tea, Jasmine Green, Plastic Bottles", '1'), ('-975890652', "Ito En Teas\\' Tea, Lemongrass Green, Plastic Bottles", '1'), ('-1152939818', "Ito En Teas\\' Tea, Pure Green, Plastic Bottles", '1'), ('19102859', 'LaCroix Sparkling Water, Coconut, Cans', '1'), ('-546157568', 'LaCroix Sparkling Water, Grapefruit, Cans', '1')]] 
    list2 = [[('Coca-Cola Zero Sugar Cola, Cans', 4), ("Ito En Teas\\' Tea, Jasmine Green, Plastic Bottles", 3), ("Ito En Teas\\' Tea, Pure Green, Plastic Bottles", 5)]]

    does_match = solution(list1, list2)
    print(does_match)

main()

这将返回True

以下是您要查找的内容:

因为我们可以假设列表中有一个包含元组的列表,所以应该这样做:

def函数():
对于索引,枚举中的项(列表1[0]):
对于索引,枚举中的项2(列表2[0]):
对于第项中的第3项:
对于第2项中的第4项:
如果项目3==项目4:
返回真值

这是假设您的list1和list2变量就是您发布的变量。我很难实现这个解决方案,尽管从概念上我可以看到它在工作。你认为我做错了什么?它返回'none'而不是True或False。但是你能用元组来实现吗?我在元组数组中寻找要匹配的元组元素。我的数组看起来像这样的原因是因为它们在我的程序中是这样的。上下文:我从一个抓取的网页和一个excel电子表格中获取元组列表。如果array1中的任何元组中的任何元素与array2中的任何元组中的任何元素匹配,是否应该这样做?或者,如果数组1中的元组与数组2中的元组匹配,则应该是?更多地定义需求,我可以帮你想出一个解决办法(嗯,仔细考虑一下,我需要让名称相互匹配。例如:数组1中元组中的元素“可口可乐零糖可乐,罐头”与数组2中元组中的元素“可口可乐零糖可乐,罐头”匹配。@TrentXavier这使用algoriyhtsmts解决方案工作。请让我们知道这是否有效。@algoriyhtsmts使用此实现是否有效。非常感谢各位,我很感谢你们的帮助。你们能把你们正在比较的实际清单贴出来吗?例如,列表1将整数作为字符
'1'
,列表2将它们作为整数
1