如果列表具有相同的值,则使用zip将两个列表组合为键值对 [英] Combine two list as key value pair using zip if lists have same values

查看:46
本文介绍了如果列表具有相同的值,则使用zip将两个列表组合为键值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列表:

yy = ['Inside the area', 'Inside the area', 'Inside the area', 'Inside the area', 'Inside the area', 'Inside the area', 'Outside the area']
lat_ = [77.2167, 77.25, 77.2167, 77.2167, 77.2, 77.2167, 77.2]

我正在使用以下文章中给出的解决方案来组合它们:

I am combining them using the solution given on some posts here by:

new_dict = {k: v for k, v in zip(yy, lat_)}
print new_dict

但是我的输出是 {区域内":77.2167,区域外":77.2}

之所以会这样,是因为大多数键是相同的.之所以这样做,是因为我想映射这两个列表,以便获得哪些lat值位于内部或外部,然后仅保留那些位于内部的值.

This is happening because the most of the keys are same. I am doing this because I want to map these two list in order to get which values of lat falls inside or outside, and then keep only the ones which fall inside.

推荐答案

您可以压缩它们并在列表理解中添加防护:

You can zip them and add a guard inside a list comprehension:

res = [lat for a, lat in zip(yy, lat_) if a.startswith("Inside")]

这篇关于如果列表具有相同的值,则使用zip将两个列表组合为键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆