首页 > 解决方案 > 将列表与元组列表进行比较

问题描述

我有一个元组列表,例如

journeylist = [("uk_Frank_3734823","342-2432-242-2342",2,3434-3434),("uk_joe_3734823","342-2432-242-2342",2,3434-3434)]

和一个列表,例如

exclusion = ["joe","jack","alice"]

我想将排除列表与每个元组的 0 索引进行比较。该值在任何情况下都可以是,例如73473_Jack_uk,列表具有jack. 他们应该匹配。如果存在匹配,则必须删除整个元组或将非匹配添加到另一个列表中。

标签: pythonpython-3.x

解决方案


我会通过降低第一个值的大小写并exclusion对照它检查列表来做到这一点:

lowered_exclusion = [excl.lower() for excl in exclusion]
filtered_journeylist = []
for journey in journeylist:
    first = journey[0].lower()
    if not any(excl in first for excl in lowered_exclusion):
        filtered_journeylist.append(journey)

推荐阅读