为什么这个 lambda 表达式结果对象的 dict 转换有什么问题?

Why what's wrong with this dict conversion of a lambda expression result object?

自鸣得意地认为我拥有宇宙中最好的 lambda 表达式return 使用 python 和 netifaces

需要的所有相关网络信息
>>> list(map(lambda interface: (interface, dict(filter(lambda ifaddress: ifaddress in (netifaces.AF_INET, netifaces.AF_LINK), netifaces.ifaddresses(interface) )))  , netifaces.interfaces()))

但我明白了

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <lambda>
TypeError: cannot convert dictionary update sequence element #0 to a sequence

缩小一点

>>>dict(filter(lambda ifaddress: ifaddress in (netifaces.AF_INET, netifaces.AF_LINK), netifaces.ifaddresses("eth0")))

问题所在:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot convert dictionary update sequence element #0 to a  sequence

但这样我就可以将过滤器对象转换为列表

 >>> list(filter(lambda ifaddress: ifaddress in (netifaces.AF_INET, netifaces.AF_LINK), netifaces.ifaddresses("eth0")))
 [17, 2]

但是,这不是我想要的。我想要它实际上是什么:

>>> netifaces.ifaddresses("tun2")
{2: [{'addr': '64.73.0.0', 'netmask': '255.255.255.255', 'peer': '192.168.15.4'}]}
>>> type (netifaces.ifaddresses("eth0"))
<class 'dict'>

那么是什么让我的演员表变回字典?

当给定字典作为输入时,filter 将仅迭代并 return 来自该字典的 keys

>>> filter(lambda x: x > 1, {1:2, 3:4, 5:6})
[3, 5]

因此,您只是将过滤后的键序列提供给新字典,而不是键值对。您可以这样修复它:注意对 items() 的调用以及内部 lambda 如何获取元组作为输入。

list(map(lambda interface: (interface, dict(filter(lambda tuple: tuple[0] in (netifaces.AF_INET, netifaces.AF_LINK), 
                                                   netifaces.ifaddresses(interface).items()))), 
         netifaces.interfaces()))

这不是很漂亮...我建议将您的代码更改为嵌套列表和字典理解:

[(interface, {ifaddress: value 
          for (ifaddress, value) in netifaces.ifaddresses(interface).items()
          if ifaddress in (netifaces.AF_INET, netifaces.AF_LINK)})
 for interface in netifaces.interfaces()]