【发布时间】:2018-01-23 05:51:44
【问题描述】:
我有一个包含主机名键和列表值的主机字典。我希望能够从每个值的列表中删除任何 fruit_ 项目。
host = {
'abc.com': ['fruit_apple', 'fruit_orange', 'veg_carrots'],
'123.com': None,
'foo.com': ['fruit_tomatoes', 'veg_potatoes']
}
for v in host.values():
if v is not None:
for x in v:
try:
# creating my filter
if x.startswith('fruit_'):
# if x finds my search, get, or remove from list value
host(or host.value()?).get/remove(x)# this is where i'm stuck
print(hr.values(#call position here?)) # prove it
except:
pass
我被困在评论区,我觉得我错过了另一个迭代(某个地方的新列表?),或者我不明白如何写回列表值。任何方向都会有所帮助。
【问题讨论】:
-
在您的示例中,
if v is not None:将始终为True。你可能已经知道了 -
是的,没关系。我猜你在
host的值中有一个None没有发布?我只是想确保您认为它不会捕获[None] -
我想我应该更新一下,''123.com': [ None ], ' 应该是 '123.com': None,不在列表中。
-
没问题,我不是故意挑剔的,我猜你已经知道了!
标签: python python-3.x list dictionary