从主词典中删除子词典 [英] deleting a sub dictionary from a main dictionary
本文介绍了从主词典中删除子词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以从主词典中删除子词典,而无需比较键和值或编写for循环?
>
有可用的方法吗?
主要词典:
>>> dict1 = {1:100,2:30,4:400,5:60}
Sub Dict:
>>> dict2 = {1:100,5:60}
O / P:
应该是
dict1 = {2: 30,4:400}
类似列表。
谢谢
PSB
解决方案
如果你不想使用for循环,那么使用set和dict()
展开 | 选择 | Wrap | 行号
Python 2.3.5(#1,2006年8月19日,21:31:42)
[GCC 4.0.1(Apple Computer,Inc。build 5363) )] on darwin
输入help,copyright,credit等等。或许可证或了解更多信息。
>>> x = {1:2,3:4,5:6}
>>> y = {10:11,3:4,14:15}
>>> dir()
[''__ builtin__'',''__ doc__'',''_ _ 1 _'',''x'',''y'']
>>> x.items()
[(1,2),(3,4),(5,6)]
>>> a = set(x.items())
Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,in?
NameError:未定义名称''set''
>>> a = set(x.items())
Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,in?
NameError:未定义名称''set''
>>> dict1 = {1:100,2:30,4:400,5:60}
>>> dict2 = {1:100,5:60}
>>> a = set(dict1.items())
回溯(最近一次调用最后一次):
文件"< stdin>",第1行,在?
NameError:名称''set''未定义
我试了但是给了我以下错误,你正在使用哪个版本的python
太糟糕了,它仅在2.4中可用。请参阅此处
Hi,
Is it possible to delete a sub dictionary from a main dictionary,without comparing the keys and values or writing a for loop?.
Is there method available?
Main Dict:
>>> dict1= {1:100,2:30,4:400,5:60}
Sub Dict:
>>> dict2 = {1:100,5:60}
O/P:
Should be
dict1= {2:30,4:400}
similary for list.
Thanks
PSB解决方案if you don''t want to use for loops, then use set and dict()
Expand|Select|Wrap|Line Numbers
Python 2.3.5 (#1, Aug 19 2006, 21:31:42)
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = {1:2, 3:4, 5:6}
>>> y = { 10:11, 3:4, 14:15}
>>> dir()
[''__builtins__'', ''__doc__'', ''__name__'', ''x'', ''y'']
>>> x.items()
[(1, 2), (3, 4), (5, 6)]
>>> a = set(x.items())
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name ''set'' is not defined
>>> a = set(x.items())
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name ''set'' is not defined
>>> dict1= {1:100,2:30,4:400,5:60}
>>> dict2 = {1:100,5:60}
>>> a = set(dict1.items())
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name ''set'' is not defined
I tried it but gave me the following error, which version of python are you using
too bad, its only available in 2.4 . See here
这篇关于从主词典中删除子词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取
|
15天全站免登陆