python 删除字典中列表_Python:从列表中删除字典,给出要删除的键列表(Python: remove dictionary from list, given list of keys to ...

Python:从列表中删除字典,给出要删除的键列表(Python: remove dictionary from list, given list of keys to remove)

我有一个扩展问题从列表中删除字典 ,除了我有一个字典键列表我想从列表中删除。

所以,我有类似的东西:

a=[{'id': 1, 'name': 'paul'},{'id': 2, 'name': 'john'},{'id': 3, 'name': 'john2'},{'id': 4, 'name': 'johnasc'}]

现在,我有一个del_list,就像这样:

del_id_list=[2,4]

什么是有效的方法(假设列表a是大的)从a删除id为del_list词典?

I have an extension question to remove dictionary from list, except that I have a list of dictionary keys I would like to remove from the list.

So, I have something like:

a=[{'id': 1, 'name': 'paul'},{'id': 2, 'name': 'john'},{'id': 3, 'name': 'john2'},{'id': 4, 'name': 'johnasc'}]

now, I have a del_list, like so:

del_id_list=[2,4]

what is the efficient way (assuming list a is LARGE) to delete dictionaries with id from del_list from a?

原文:https://stackoverflow.com/questions/41768220

2020-01-22 12:01

满意答案

熟悉过滤器 :

result = filter(lambda x: x['id'] not in del_id_list,a)

编辑:

关于del_id_list本身,如果它很长,你可能想要考虑in语句的复杂性。 也许set甚至一个dict (具有任意值)会更好。 检查一下 。

编辑2:正如@Jean正确指出的那样,这是Py3中的迭代序列。 只需添加清单:

result = list(filter(lambda x: x['id'] not in del_id_list,a))

Get acquainted with filter:

result = filter(lambda x: x['id'] not in del_id_list,a)

EDIT:

Regarding the del_id_list itself, if it's long you may want to consider the complexity of the in statement. Maybe a set and even a dict (with arbitrary value) would be better. Check this.

EDIT 2: As @Jean correctly points out, this is a iteration sequence in Py3. Just add list:

result = list(filter(lambda x: x['id'] not in del_id_list,a))

2017-01-20

相关问答

您可以使用Enumerable.Except来查找不在字典中的键: foreach (var key in MyDictionary.Keys.Except(AllowedList).ToList())

MyDictionary.Remove(key);

ToList()创建一个新的设置差异列表并防止发生异常。 You could use Enumerable.Except to find the keys that are not in the dictionary: foreach ...

只需使用all来测试列表中的所有项目是否都在字典值中,并在列表理解的过滤器中使用它: lst = [' 5 ', ' 4 ']

results[:] = [d for d in results if all(i in d['text'] for i in lst)]

print(results)

# [{'text': 'String 5 - 4 - 1', 'id': 3}]

Simply use all to test if all the items in the list are in ...

使用dict理解: >>> myDict = {0: [0, 1, 2], 1: [], 2: [20, 25, 26, 28, 31, 34], 3: [], 4: [0, 1, 2, 3, 4, 7, 10], 5: [], 6: [10, 20, 24]}

>>> myDict = {k: v for k, v in myDict.items() if v}

>>> myDict

{0: [0, 1, 2], 2: [20, 25, 26, 28, 31, 34], 4: [0, 1, 2,...

迭代时,您无法修改列表。 您可以使用列表推导来过滤掉链接: for item, links in links_ref.items():

links_ref[item] = [link for link in links if link in characters]

You can't modify the list while you are iterating over it. You can make use of list comprehensions though in orde...

你可以使用简单的dict理解: myDict = {key:val for key, val in myDict.items() if val != 42}

因此: >>> {key:val for key, val in myDict.items() if val != 42}

{8: 14, 1: 'egg'}

You can use a simple dict comprehension: myDict = {key:val for key, val in myDict.items() ...

你可以在字典理解中使用列表理解,如下所示: myDict = {k:[el for el in v if el != 'x'] for k, v in myDict.items()}

print(myDict)

产量 {0: ['id1', 'id2', 'id3'], 1: ['id1', 'id2'], 2: ['id1']}

You could use a list comprehension within a dictionary comprehension as follows: m...

a = [x for x in a if x['link'] not in b]

演示: >>> a = [{'link':'http://example.com/1/', 'id': 1}, {'link':'http://example.com/2/', 'id': 2}]

>>> b = ['http://example.com/2/', 'http://example.com/3/']

>>> a = [x for x in a if x['link'] not in b]

>>> a

...

尝试创建字典理解然后获取它的值然后将其转换为列表: data = [{'url': 'www.url1.com', 'max': '14.6'},

{'url': 'www.url2.com', 'max': '17.8'},

{'url': 'www.url2.com', 'max': '18.4'},

{'url': 'www.url3.com', 'max': '15.5'}]

print(list({v['url']:v for v in d...

熟悉过滤器 : result = filter(lambda x: x['id'] not in del_id_list,a)

编辑: 关于del_id_list本身,如果它很长,你可能想要考虑in语句的复杂性。 也许set甚至一个dict (具有任意值)会更好。 检查一下 。 编辑2:正如@Jean正确指出的那样,这是Py3中的迭代序列。 只需添加清单: result = list(filter(lambda x: x['id'] not in del_id_list,a))

Get acq...

这可能是一种更简单的方法: for index, Player in enumerate(PlayerID):

if Player['ID'] == SearchID:

PlayerID.pop(index)

print("The user with ID", SearchID, ", has been deleted")

break

else:

print("The ID provided does not exist.")

Th...

相关文章

列表就像java里的collection,所具有的特性也要比元组更多,更灵活,其character总结

...

我们现有的Hadoop集群已经运行了一段时间了 由于集群中的服务器分布在2个不同的机房,受跨机房带宽的

...

我们上一节认识了FreeMarker基本数据类型,接口认识FreeMarker集合(List、Map)

...

从数据库查询N条记录放在List集合中,然后通过request对象返回给页面,通过循环遍历将List中

...

Windowsis an extremely effective and a an efficient

...

java List对象排序有多种方法,下面是其中两种 第一种方法,list中的对象实现Comparab

...

源码解读Mybatis List列表In查询实现的注意事项 在SQL开发过程中,动

...

各位有经验的Javaer,请问使用Map、List这种集合类做缓存时,需要 每天清空一次数据,那么我是

...

python里的字典就像java里的HashMap,以键值对的方式存在并操作,其特点如下:通过键来存取

...

List按对象进入的顺序保存对象,不做排序或编辑操作。Set对每个对象只接受一次,并使用自己内部的排序

...

最新问答

如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re

第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型

这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;

问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是

我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar

Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/

你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV

12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar

这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定


版权声明:本文为weixin_39948442原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。