我正在尝试合并两个字典。其中之一是空的。我在Python 2.x中有一个代码。但是我正在转换3.7中的代码。我确实进行了转换。但是我不知道我是对还是错。因为我完全是新手。 该代码实际上是一个视频字幕模型。下面是代码片段。如果我做对了,请帮助我澄清一下。
我已经转换了3.7中的代码。但是我不确定我做对与否。
class DataProvision:
def __init__(self, options):
assert options['batch_size'] == 1
self._options = options
self._splits = {'train':'train', 'val':'val_1'}
np.random.seed(options['random_seed'])
random.seed(options['random_seed'])
self._ids = {} # video ids
captions = {}
self._sizes = {}
print('Loading paragraph data ...')
for split in self._splits:
tmp_ids =
open(os.path.join(self._options['caption_data_root'], split,
'ids.txt'), 'r').readlines()
tmp_ids = [id.strip() for id in tmp_ids]
self._ids[split] = tmp_ids
self._sizes[split] = len(self._ids[split])
tmp_captions =
json.load(open(os.path.join(self._options['caption_data_root'],
split, 'encoded_sentences.json'), 'r'))
captions[split] = {tmp_ids[i]:tmp_captions[i] for i in
range(len(tmp_ids))}
# merge two caption dictionaries
self._captions = {}
for split in self._splits:
#self._captions = dict(self._captions.items() +
#captions[split].items()) #2.x original code line
self._captions = {**self._captions, **captions[split]} #3.7
#convert the above line in 3.7
我将不胜感激。是否清楚地复制了原始代码。因为我在进一步的代码中遇到错误。谢谢