如果城市在我的list_cities中,我想在我的地址栏中返回城市。
Cities = ['Los Angeles','New York']
Address = ['New York 123 ave unit 804','Los Angeles 567 ave unit 701']
if any(city in address for city in Cities):
print ()
我想要回归['纽约','洛杉矶'],有什么方法可以做到这一点吗? 谢谢!
答案 0 :(得分:3)
您可以代替Cities
进行迭代:
Cities = ['Los Angeles','New York']
Address = ['New York 123 ave unit 804','Los Angeles 567 ave unit 701']
final_cities = [i for i in Cities if any(i in b for b in Address)]
根据Address
中的位置排序的解决方案:
new_Address = {a:[c for c, d in enumerate(Address) if a in d] for a in Cities}
final_address = map(lambda x:x[0], sorted({a:b for a, b in new_Address.items() if b}.items(), key=lambda x:x[-1]))
输出:
['New York', 'Los Angeles']