我正在尝试重命名一些键并将分组键的值分组。我的内容如下:
text_image_old = {10_pdf 10_pdf0: "some text", 10_pdf 10_pdf1: "more text", 10_pdf 10_pdf2: "even more text"}
使用正则表达式,我可以迭代地替换名称,这样只剩下10_pdf,但是由于循环,文本将只包含值“甚至更多的文本”(例如最后一个值):
text_image_new = {re.sub('[a-zA-Z0-9_]+.pdf[0-9]', '', k): v for k, v in text_image_old.items()}
我该如何替换键并将值分组?谢谢!
编辑:预期输出应如下所示
text_image_new = {10_pdf :"some text" "more text" "even more text"}
或者更容易获得:
text_image_new = {10_pdf :"some text more text even more text"}
答案 0 :(得分:1)
我希望这对您有用,或者至少有助于解决您的问题:
text_image_old = {'10_pdf 10_pdf0': "some text", '10_pdf 10_pdf1': "more text",\
'10_pdf 10_pdf2': "even more text"}
new_dict = {}
for k, v in text_image_old.items():
k = k.split(' ')[0]
if k in new_dict:
new_dict[k] += v + ' '
else:
new_dict[k] = v + ' '
print(new_dict)