从包含python中字典的列表中删除元素
我有一个python脚本,它将字典存储在列表变量“data”中:从包含python中字典的列表中删除元素,python,list,dictionary,Python,List,Dictionary,我有一个python脚本,它将字典存储在列表变量“data”中: [ { 'hp':'cdldfdbc07s001:10000', 'n':'cdldfdbc07s001', 'rsid':'cdl-dit1', 'v':'3.0.12' }, { 'hp':'cdldfdbc07s002:10000', 'n':'cdldfdbc07s002', 'rsid':'cdl-dit1'
[
{
'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.遍历列表中的所有元素,并删除“hp”中包含“jass”的元素
我的代码似乎没有删除元素,我的len(data)返回的数字与以前相同
s=0
while s< len(data):
for i in ((data[s]['hp'])):
if "jass" in i:
data.remove(s)
s += 1
print len(data)
要删除python字典条目--
要删除python字典条目-- 使用
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
您也可以使用列表理解在一行中完成此操作:
[item for item in data if "jass" not in item["hp"]]
[数据。删除数据[:]中d的(d),如果d['hp']中的'jass'。]
使用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
您也可以使用列表理解在一行中完成此操作:
[item for item in data if "jass" not in item["hp"]]
[data.remove(d)for d in data[:]如果d['hp']中的'jass',
解决这些筛选问题的最佳方法是使用列表理解来重建DICT列表,其中的值中没有jass:
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"]]
更准确在这些过滤问题中,最好的方法是使用列表理解来重建DICT列表,而不使用值中的JAS:
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"]]
更准确您需要从列表中删除项目,而不是从列表中的dict中删除项目:
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'}]
您需要从列表中删除项目,而不是从列表中的dict中删除项目:
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'}]
如果您选择肯定(考虑保留哪些项目)而不是否定(删除某些项目),您可以使用列表:
[item for item in data if "jass" not in item["hp"]]
如果您选择肯定(考虑保留哪些项目)而不是否定(删除某些项目),您可以使用列表:
[item for item in data if "jass" not in item["hp"]]
发布预期结果post预期结果Top希望从列表中删除项而不是从dictOP中删除项而不是从dictOP中删除项您不应该使用列表理解来产生副作用,也就是说。我不确定我是否理解,哪些副作用?您正在创建一个没有任何副作用的列表,你不应该对副作用使用列表理解,也就是说。我不确定我是否理解,哪些副作用?你在创建一个毫无意义的列表,而不是循环。