如何进行单行dict删除操作

时间:2015-07-22 01:50:42

标签: python list list-comprehension

有没有办法在一行中执行以下操作?

[del item for item in new_json if item['Country'] in countries_to_remove]

上面给了我一个SyntaxError

2 个答案:

答案 0 :(得分:4)

del is a statement您无法使用as an expression in list comprehenstion。这就是为什么你得到java.lang.OutOfMemoryError: PermGen space at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:800) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:421) at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:383) at org.codehaus.jackson.map.ObjectMapper.<init>(ObjectMapper.java:390) at org.codehaus.jackson.map.ObjectMapper.<init>(ObjectMapper.java:358) at org.codehaus.jackson.map.ObjectMapper.<init>(ObjectMapper.java:328) at org.apache.hadoop.hbase.client.Operation.<clinit>(Operation.java:40) at org.apache.hadoop.hbase.client.MetaScanner.metaScan(MetaScanner.java:165) at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.prefetchRegionCache(HConnectionManager.java:1158) at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.locateRegionInMeta(HConnectionManager.java:1222) at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.locateRegion(HConnectionManager.java:1110) at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.locateRegion(HConnectionManager.java:1067) at org.apache.hadoop.hbase.client.HTable.finishSetup(HTable.java:326) at org.apache.hadoop.hbase.client.HTable.<init>(HTable.java:192) at org.apache.hadoop.hbase.client.HTable.<init>(HTable.java:165) at org.springframework.data.hadoop.hbase.HbaseUtils.getHTable(HbaseUtils.java:76) at org.springframework.data.hadoop.hbase.HbaseTemplate.getTable(HbaseTemplate.java:75) at org.springframework.data.hadoop.hbase.HbaseTemplate.execute(HbaseTemplate.java:54) at org.springframework.data.hadoop.hbase.HbaseTemplate.get(HbaseTemplate.java:170) at org.springframework.data.hadoop.hbase.HbaseTemplate.get(HbaseTemplate.java:160) at edu.mayo.registry.fhir.rest.hbase.repository.HbaseRepository.partialScan(HbaseRepository.java:50) at edu.mayo.registry.fhir.rest.controller.ESController.putIndexValue(ESController.java:61) at edu.mayo.registry.fhir.rest.controller.ESController$$FastClassBySpringCGLIB$$729642a0.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)

您可以使用列表理解来创建新列表,而不包含您不想要的元素,例如

SyntaxError

这实际上等同于

[item for item in new_json if item['Country'] not in countries_to_remove]

这种操作称为过滤列表,您可以使用内置result = [] for item in new_json: if item['Country'] not in countries_to_remove: result.append(item) 函数,如下所示

filter

正如mgilson in the comments section所建议的那样,如果您只想改变原始列表,那么您可以使用切片赋值,就像这样

list(filter(lambda x: x['Country'] not in countries_to_remove, new_json))

答案 1 :(得分:4)

del是python中的一个语句,你不能在列表理解中有语句(你只能在那里有表达式)。为什么不将new_json创建为不包含要删除的项目的新列表或字典。示例=

new_json = [item for item in new_json if item['Country'] not in countries_to_remove]