我是python3新手,每次匹配都很难从两个列表中返回值。你知道吗
locations = [("ngv", 4, 0), ("town hall", 4, 4),("myhotel", 2, 2), ("parliament", 8, 5.5), ("fed square", 4, 2)]
tour = ["ngv", "fed square", "myhotel"]
我的代码会找到匹配项,但不会返回位置坐标。你知道吗
['ngv', 'fed square', 'myhotel']
我现在的代码是:
places = [u[0] for u in locations]
new = [i for i in tour if i in places]
print(new)
您不需要中间列表理解,只需:
注意:如果
locations
和tour
包含许多项,那么可以先将tour
设为set
,例如tour = set(tour)
,从而加快代码速度并降低时间复杂度相关问题 更多 >
编程相关推荐