help(difflib.get_close_matches)
Help on function get_close_matches in module difflib:
get_close_matches(word, possibilities, n=3, cutoff=0.6)
Use SequenceMatcher to return list of the best "good enough" matches.
word is a sequence for which close matches are desired (typically a
string).
possibilities is a list of sequences against which to match
word(typically a list of strings).
我可以将dict.keys()用作get_close_matches中的参数“ possibilities”,其中“ possibilities”需要一个列表。但是为什么我不能像普通的列表一样访问dict.items()像a [0],a [1](a是一个列表)?
答案 0 :(得分:3)
在Python 3中,dict.items()
(还有.keys()
和.values()
)返回一个特殊的dictionary view object。它的行为就像一个迭代器,但不是专门的列表。
#!/usr/bin/env python3
d = {}
d['a'] = 1
d['b'] = 2
# You can pack items() into a list and then it's a "real" list
l = list(d.items())
print(repr(l[1]))
# Or you can use itertools or otherwise use it as a plain iterator
import itertools
for p in itertools.islice(d.items(), 1, 2):
print(repr(p))
答案 1 :(得分:2)
Dict.items()不返回列表。相反,它返回一类dict_items。如果您仅对键感兴趣,请使用Dict.keys()。您无法使用索引方法访问字典