Python - 从另一个减去一个列表 [英] Python - Subtract a list of dicts from another

查看:1240
本文介绍了Python - 从另一个减去一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们有兴趣比较多个列表,区分差异并重复一遍。 'ssid' - str,'bssid' - str,'channel' - int,'flags' - list,'found' - bool



/ p>

  list = list(set(networks_list).difference(missing_networks))
pre>

但是我收到错误:

  unhashable type'dict '

我的数据结构如下所示:

 列表:[{'found':False,'flags':['WPA2-PSK-CCMP','WPS','ESS'],'ssid':'SOHO_BROADCAST' ,'bssid':'30:46:9a:9d:11:1a','channel':1},{'found':False,'flags':['WPA-EAP-TKIP','WPA2-EAP -C','ESS'],'ssid':'Cisco 2.4ghz','bssid':'40:f4:ec:7f:3c:5a','channel':11},{'found':False ,'flags':['WP A-EAP-TKIP','WPA2-EAP-CCMP','ESS'],'ssid':'Cisco 5.0ghz','bssid':'40:f4:ec:7f:3c:54' ':149}] 

缺少网络最初是空的。



有没有一种简单的方法?

解决方案

,但是如果你的字典大部分是原始的,而不是很大,你可以这样做:



假设你的数据看起来像这样:

  networks = [
{'address':'192.168.1.1'},
{'地址':'127.0.0.1 '},
]

missing = [
{'address':'127.0.0.1'}
]

您可以将字典列表转换为列表元组(可以是哈希)

  def make_hashable(d):
return(frozenset(x.iteritems())for d在d)

net work_hashable = make_hashable(networks)
missing_hashable = make_hashable(missing)

然后减去

  diff = set(networks_hashable).difference(missing_hashable)

现在您有一个元组列表

 打印列表(diff)

或转换回词典

  print [dict(x)for x in diff] 

更新



我已经根据@ gnibbler的评论更改了 make_hashable 的定义。


I'm interested in comparing multiple lists, taking the difference and iterating through that.

Both are list of dicts that contain the following keys: 'ssid' - str, 'bssid' - str, 'channel' - int, 'flags' - list, 'found' - bool

I've tried:

 list = list(set(networks_list).difference(missing_networks))

But I receive the error:

unhashable type 'dict'

My data structure looks like this:

list: [{'found': False, 'flags': ['WPA2-PSK-CCMP', 'WPS', 'ESS'], 'ssid': 'SOHO_BROADCAST', 'bssid': '30:46:9a:9d:11:1a', 'channel': 1}, {'found': False, 'flags': ['WPA-EAP-TKIP', 'WPA2-EAP-CCMP', 'ESS'], 'ssid': 'Cisco 2.4ghz', 'bssid': '40:f4:ec:7f:3c:5a', 'channel': 11}, {'found': False, 'flags': ['WPA-EAP-TKIP', 'WPA2-EAP-CCMP', 'ESS'], 'ssid': 'Cisco 5.0ghz', 'bssid': '40:f4:ec:7f:3c:54', 'channel': 149}]

Missing networks is initially empty.

Is there a simple way of doing this?

解决方案

There are probably many pitfalls to a generic approach like this, but if your dictionaries are of mostly primitives, and not huge, you can do something like this:

Assuming your data looks something like this:

networks = [
        {'address': '192.168.1.1'},
        {'address': '127.0.0.1'},
    ]

missing = [
        {'address': '127.0.0.1'}
    ]

You can turn the lists of dictionaries into lists tuples (which are hashable)

def make_hashable(d):
    return (frozenset(x.iteritems()) for x in d)

networks_hashable = make_hashable(networks)
missing_hashable = make_hashable(missing)

Then subtract

diff = set(networks_hashable).difference(missing_hashable)

Now you have a list of tuples

print list(diff)

or, convert back to dictionaries

print [dict(x) for x in diff]

Update

I've changed the definition of make_hashable based on @gnibbler's comment.

这篇关于Python - 从另一个减去一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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