for key, val in my_dict.items():
# no garantee we have string pair as key here
try:
firstname, lastname = key
except ValueError:
# not a pair...
continue
# this would require another try/except block since
# equality test on different types can raise anything
# but let's pretend it's ok :-/
if firstname == "john":
do_something_with(key, val)
尝试:
它会迭代所有的键,因此可能不是最有效的方法,但我将简单地说明一下显而易见的方法,以防您忽略它:
坏消息:你不能(至少不能直接)。您所做的不是一个“二维”dict,而是一个以元组(在您的例子中是字符串对)作为键的dict,并且只使用键的散列值(通常用于哈希表)。您需要的是顺序查找,即:
不用说,这有点挫败了使用口述错误的全部意义。。。使用适当的关系数据库怎么样?在
相关问题 更多 >
编程相关推荐