我有一个python脚本,它将一个字典存储在一个列表变量' data' :
[
{
'hp':'cdldfdbc07s001:10000',
'n':'cdldfdbc07s001',
'rsid':'cdl-dit1',
'v':'3.0.12'
},
{
'hp':'cdldfdbc07s002:10000',
'n':'cdldfdbc07s002',
'rsid':'cdl-dit1',
'v':'3.0.12'
},
{
'hp':'cdldfdbc07s001:11000',
'n':'cdldfdbc07s001',
'rsid':'cdl-fit1',
'v':'3.0.12'
},
{
'hp':'cdldfdbc07s002:11000',
'n':'cdldfdbc07s002',
'rsid':'cdl-fit1',
'v':'3.0.12'
},
{
'hp':'cdldvjassvp073:10000',
'n':'cdldvjassva073',
'rsid':'mobile',
'v':'2.6.3'
},
{
'hp':'cdldvjassva072:10000',
'n':'cdldvjassva072',
'rsid':'mobile',
'v':'2.6.3'
},
{
'hp':'cdldvjassva074:11002',
'n':'cdldvjassva074',
'rsid':'cache2',
'v':'2.6.3'
},
{
'hp':'cdldfdbc05s002:11000',
'n':'cdldfdbc05s002',
'rsid':'dit2',
'v':'3.0.9'
}
]
我想
1.通过列表的所有元素并删除包含" jass"的元素。在' hp'。
我的代码似乎没有删除元素,我的len(数据)返回与之前相同的数字。
s=0
while s< len(data):
for i in ((data[s]['hp'])):
if "jass" in i:
data.remove(s)
s += 1
print len(data)
预期结果列表:
[
{
'hp':'cdldfdbc07s001:10000',
'n':'cdldfdbc07s001',
'rsid':'cdl-dit1',
'v':'3.0.12'
},
{
'hp':'cdldfdbc07s002:10000',
'n':'cdldfdbc07s002',
'rsid':'cdl-dit1',
'v':'3.0.12'
},
{
'hp':'cdldfdbc07s001:11000',
'n':'cdldfdbc07s001',
'rsid':'cdl-fit1',
'v':'3.0.12'
},
{
'hp':'cdldfdbc07s002:11000',
'n':'cdldfdbc07s002',
'rsid':'cdl-fit1',
'v':'3.0.12'
},
{
'hp':'cdldfdbc05s002:11000',
'n':'cdldfdbc05s002',
'rsid':'dit2',
'v':'3.0.9'
}
]
答案 0 :(得分:3)
data = [{'hp': 'cdldfdbc07s001:10000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-dit1', 'v': '3.0.12'}, {'hp': 'cdldfdbc07s002:10000', 'n': 'cdldfdbc07s002', 'rsid': 'cdl-dit1', 'v': '3.0.12'}, {'hp': 'cdldfdbc07s001:11000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-fit1', 'v': '3.0.12'}, {'hp': 'cdldfdbc07s002:11000', 'n': 'cdldfdbc07s002', 'rsid': 'cdl-fit1', 'v': '3.0.12'},{'hp': 'cdldvjassvp073:10000', 'n': 'cdldvjassva073', 'rsid': 'mobile', 'v': '2.6.3'}, {'hp': 'cdldvjassva072:10000', 'n': 'cdldvjassva072', 'rsid': 'mobile', 'v': '2.6.3'}, {'hp': 'cdldvjassva074:11002', 'n': 'cdldvjassva074', 'rsid': 'cache2', 'v': '2.6.3'}, {'hp': 'cdldfdbc05s002:11000', 'n': 'cdldfdbc05s002', 'rsid': 'dit2', 'v': '3.0.9'}]
使用remove
从列表中删除元素:
print(len(data)) # prints 8
for d in data[:]: # iterate over a COPY of the list[:]
if 'jass' in d['hp']:
data.remove(d)
print(len(data)) # prints 5
您也可以使用列表理解在单行中执行此操作:
[data.remove(d) for d in data[:] if 'jass' in d['hp']]
答案 1 :(得分:3)
这些过滤问题的最佳方法是使用列表推导来重建值中没有jass的dicts列表:
data = [{'hp': 'cdldfdbc07s001:10000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-dit1', 'v': '3.0.12'}, {'hp': 'cdldfdbc07s002:10000', 'n': 'cdldfdbc07s002', 'rsid': 'cdl-dit1', 'v': '3.0.12'}, {'hp': 'cdldfdbc07s001:11000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-fit1', 'v': '3.0.12'}, {'hp': 'cdldfdbc07s002:11000', 'n': 'cdldfdbc07s002', 'rsid': 'cdl-fit1', 'v': '3.0.12'},
{'hp': 'cdldvjassvp073:10000', 'n': 'cdldvjassva073', 'rsid': 'mobile', 'v': '2.6.3'}, {'hp': 'cdldvjassva072:10000', 'n': 'cdldvjassva072', 'rsid': 'mobile', 'v': '2.6.3'}, {'hp': 'cdldvjassva074:11002', 'n': 'cdldvjassva074', 'rsid': 'cache2', 'v': '2.6.3'},
{'hp': 'cdldfdbc05s002:11000', 'n': 'cdldfdbc05s002', 'rsid': 'dit2', 'v': '3.0.9'}]
data_without_jass = [d for d in data if all("jass" not in v for v in d.values())]
print(data_without_jass)
结果:
[{'hp': 'cdldfdbc07s001:10000', 'rsid': 'cdl-dit1', 'v': '3.0.12',
'n': 'cdldfdbc07s001'}, {'hp': 'cdldfdbc07s002:10000', 'rsid': 'cdl-dit1', 'v': '3.0.12', 'n': 'cdldfdbc07s002'}, {'hp': 'cdldfdbc07s001:11000',
'rsid': 'cdl-fit1', 'v': '3.0.12', 'n': 'cdldfdbc07s001'}, {'hp': 'cdldfdbc07s002:11000', 'rsid': 'cdl-fit1', 'v': '3.0.12', 'n': 'cdldfdbc07s002'},
{'hp': 'cdldfdbc05s002:11000', 'rsid': 'dit2', 'v': '3.0.9', 'n': 'cdldfdbc05s002'}]
这里的关键是条件:
all("jass" not in v for v in d.values())
测试每个子字典的所有值是否都不包含"jass"
(或使用反向逻辑:测试每个子字典的值是否包含"jass"
)
我现在注意到条件只检查hp密钥,所以:
[d for d in data if "jass" not in d["hp"]]
更准确
答案 2 :(得分:0)
您需要从列表中删除项目,而不是从列表中的词典中删除项目:
l = [{'hp': 'cdldfdbc07s001:10000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-dit1', 'v': '3.0.12'},
{'hp': 'cdldfdbc07s002:10000', 'n': 'cdldfdbc07s002', 'rsid': 'cdl-dit1', 'v': '3.0.12'},
{'hp': 'cdldfdbc07s001:11000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-fit1', 'v': '3.0.12'},
{'hp': 'cdldfdbc07s002:11000', 'n': 'cdldfdbc07s002', 'rsid': 'cdl-fit1', 'v': '3.0.12'},
{'hp': 'cdldvjassvp073:10000', 'n': 'cdldvjassva073', 'rsid': 'mobile', 'v': '2.6.3'},
{'hp': 'cdldvjassva072:10000', 'n': 'cdldvjassva072', 'rsid': 'mobile', 'v': '2.6.3'},
{'hp': 'cdldvjassva074:11002', 'n': 'cdldvjassva074', 'rsid': 'cache2', 'v': '2.6.3'},
{'hp': 'cdldfdbc05s002:11000', 'n': 'cdldfdbc05s002', 'rsid': 'dit2', 'v': '3.0.9'}
]
result = [x for x in l if 'jass' not in x["hp"]] # the if condition lets only dicts in
# that do not have 'jass' in its key 'hp'
print (l)
print(result)
输出:
[{'hp': 'cdldfdbc07s001:10000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-dit1', 'v': '3.0.12'},
{'hp': 'cdldfdbc07s002:10000', 'n': 'cdldfdbc07s002', 'rsid': 'cdl-di
t1', 'v': '3.0.12'},
{'hp': 'cdldfdbc07s001:11000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-fit1', 'v': '3.0.12'},
{'hp': 'cdldfdbc07s002:11000', 'n': 'cdldfdbc07s0
02', 'rsid': 'cdl-fit1', 'v': '3.0.12'},
{'hp': 'cdldvjassvp073:10000', 'n': 'cdldvjassva073', 'rsid': 'mobile', 'v': '2.6.3'},
{'hp': 'cdldvjassva072:10000', '
n': 'cdldvjassva072', 'rsid': 'mobile', 'v': '2.6.3'},
{'hp': 'cdldvjassva074:11002', 'n': 'cdldvjassva074', 'rsid': 'cache2', 'v': '2.6.3'},
{'hp': 'cdldfdbc05
s002:11000', 'n': 'cdldfdbc05s002', 'rsid': 'dit2', 'v': '3.0.9'}]
[{'hp': 'cdldfdbc07s001:10000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-dit1', 'v': '3.0.12'},
{'hp': 'cdldfdbc07s002:10000', 'n': 'cdldfdbc07s002', 'rsid': 'cdl-di
t1', 'v': '3.0.12'},
{'hp': 'cdldfdbc07s001:11000', 'n': 'cdldfdbc07s001', 'rsid': 'cdl-fit1', 'v': '3.0.12'},
{'hp': 'cdldfdbc07s002:11000', 'n': 'cdldfdbc07s0
02', 'rsid': 'cdl-fit1', 'v': '3.0.12'},
{'hp': 'cdldfdbc05s002:11000', 'n': 'cdldfdbc05s002', 'rsid': 'dit2', 'v': '3.0.9'}]
答案 3 :(得分:0)
如果你积极(想想要保留哪些项目)而不是消极(删除某些项目),你可以使用列表理解来做到这一点:
[item for item in data if "jass" not in item["hp"]]
答案 4 :(得分:-2)
删除python词典条目---
def removekey(d, key):
r = dict(d)
del r[key]
return r