Python字典重命名键和组



我正在尝试重命名一些键,并对分组键的值进行分组。我的内容如下:

text_image_old = {10_pdf 10_pdf0: "some text", 10_pdf 10_pdf1: "more text", 10_pdf 10_pdf2: "even more text"}

使用regex,我可以迭代地替换名称,这样只剩下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"}

我希望这对你有用,或者至少有助于解决你的问题:

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)

最新更新