时间:2022-07-24 10:16:05 | 栏目:Python代码 | 点击:次
>>> d {'p': 34, 'l': 54, 'b': 88} >>> for value in d.values(): ... print(value) ... 34 54 88
>>> for key,value in d.items(): ... print(key,value) ... p 34 l 54 b 88
代码
# method 1 pages = {} page = [] for img_name in os.listdir(args.image_dir): pre_str = img_name[:2] #print(pre_str) if pre_str not in pages.keys(): tmp_list = [] tmp_list.append(os.path.join(args.image_dir, img_name)) pages[pre_str] = tmp_list else: pages[pre_str].append(os.path.join(args.image_dir, img_name)) print(pages)
输出
代码
# method 2 from collections import defaultdict pages = defaultdict(list) for img_name in os.listdir(args.image_dir): pre_str = img_name[:2] #print(pre_str) pages[pre_str].append(os.path.join(args.image_dir, img_name)) print(list(pages.items())) print(pages)
输出 print(list(pages.items()))的输出
print(pages)的输出