相关疑难解决方法(0)

dict.items()和dict.iteritems()有什么区别?

dict.items()和之间是否有任何适用的差异dict.iteritems()

从Python文档:

dict.items():返回字典的(键,值)对列表的副本.

dict.iteritems():在字典(键,值)对上返回一个迭代器.

如果我运行下面的代码,每个似乎都返回对同一对象的引用.我缺少哪些微妙的差异?

#!/usr/bin/python

d={1:'one',2:'two',3:'three'}
print 'd.items():'
for k,v in d.items():
   if d[k] is v: print '\tthey are the same object' 
   else: print '\tthey are different'

print 'd.iteritems():'   
for k,v in d.iteritems():
   if d[k] is v: print '\tthey are the same object' 
   else: print '\tthey are different'   
Run Code Online (Sandbox Code Playgroud)

输出:

d.items():
    they are the same object
    they are the same object
    they are the same object
d.iteritems():
    they are the same …
Run Code Online (Sandbox Code Playgroud)

python dictionary python-2.x

673
推荐指数
6
解决办法
64万
查看次数

如何在字典或字典中查找字符串是键还是值

看起来应该很简单,但是由于某种原因我无法到达那里。

我有可能是几种可能的格式的字典:

dict1 = {"repo": "val1", "org":"val2"}
dict2 = {"key1":"repo", "key2":"org"}
dict3 = {"key1":"org and stuff"}
dict4 = {"org and stuff":"value1"}
dict5 = {"key1":{"value1":"value2"}, "key2":{"1":"org"}}
dict6 = {"org":{"value1":"value2"}, "key2":{"value1":"value2"}}
dict7 = {"org":{"subkey1":{"value1":"value2"}}, "key2":{"value1":"value2"}}
dict8 = {"key1":{"subkey1":{"value1":"org"}}, "key2":{"value1":"value2"}}
Run Code Online (Sandbox Code Playgroud)

我要搜索字符串'org',如果它在字典中的任何位置(键,值,键[subkey] [值]等),则返回true。我不希望部分字符串匹配。

也就是说,我正在寻找以下结果:

True
True
False
False
True
True
True
True
Run Code Online (Sandbox Code Playgroud)

我已经阅读了这些问题,但是没有一个答案很正确,因为我可能嵌套了一些字典:

如何从所有Dictionary.Values()中的字符串中搜索所有字符

替换字典或嵌套字典或字典列表中键值的泛型函数

查找其键与子字符串匹配的字典项

过滤字典

如何检查字符串中的字符是否在值字典中?

python dictionary

0
推荐指数
1
解决办法
55
查看次数

标签 统计

dictionary ×2

python ×2

python-2.x ×1