mar*_*est 4 python dictionary for-loop slice
这里简单的问题:
如果我尝试在 python 中对我的字典(从 xml 文件解析)进行切片,我会得到一个TypeError: unhashable type: 'slice'
for section in my_dict[:3] ['CATEGORY']['SUBCATEGORY']:
看起来字典不起作用,他们有具体的方法吗?或者是因为紧随其后的另外两个论点被误解了?
您可以使用itertools.islice:
from itertools import islice
d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
for item in islice(d, 3):  # use islice(d.items(), 3) to iterate over key/value pairs
    print(item)
输出:
a
b
c