【问题标题】:How to compare two string lists and return matches? [duplicate]如何比较两个字符串列表并返回匹配项? [复制]
【发布时间】:2017-10-28 05:09:30
【问题描述】:

我想获取两个列表并找到两个列表中出现的值。

A = ['A7', 'B4', 'B7']
B = ['A7', 'B7', 'C7', 'D7', 'E7', 'F7', 'G7', 'H7', 'I7']
returnMatches()

将返回['A7', 'B7']

【问题讨论】:

    标签: python python-3.x string-comparison


    【解决方案1】:

    你可以这样做:

    set(A) & set(B)
    

    您甚至可以从一开始就将它们构造为集合:

    A = {'A7', 'B4', 'B7'}
    

    【讨论】:

    • 这些建议都不会导致list
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    相关资源
    最近更新 更多