如何使用Python 3子集合一个字典? 使用Python 2,以下内容有效:
global pair_dict
pair_dict = {
1: 'one',
2: 'two',
3: 'three',
4: 'four',
5: 'five',
6: 'six',
7: 'seven',
8: 'eight'
}
global test_printer
def test_printer(start_chunk, end_chunk):
# .items() replacing .iteritems() from Python 2
sub_dict = dict(pair_dict.items()[start_chunk:end_chunk])
print sub_dict
test_printer(0, 2)
然而,在这个版本的Python中,我现在遇到了以下错误:
Traceback (most recent call last):
File "G:/HTPC Scripts/WebGrab Plus/TESTTESTTEST2.py", line 52, in <module>
set_chunk_start_end_points()
File "G:/HTPC Scripts/WebGrab Plus/TESTTESTTEST2.py", line 48, in set_chunk_start_end_points
test_printer(start_chunk, end_chunk)
File "G:/HTPC Scripts/WebGrab Plus/TESTTESTTEST2.py", line 27, in test_printer
sub_dict = dict(pair_dict.values()[start_chunk:end_chunk])
TypeError: 'dict_values' object is not subscriptable
sub_dict = dict(list(pair_dict.items())[start_chunk:end_chunk])
。 - Chris Doyle