gpt4 book ai didi

python - 如何重命名列表中的标签?

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:03 26 4
gpt4 key购买 nike

我有一个这样的文件

301 my name is joe
303 whatsup
306 how are you doing today
308 what happened?
308 going home
309 let's go

我想将标签 301, 303, 306, 308, 308, 309 转换为 1, 2, 3, 4, 4, 5

如何按顺序重命名这些标签,使相似的标签编号相同?

最佳答案

使用字典来存储从原始标签到新标签的映射,对于尚未映射的值,使用字典的当前len,使用setdefault

>>> labels = 301, 303, 306, 308, 308, 309
>>> names = {}
>>> for l in labels:
... names.setdefault(l, len(names)+1)
...
>>> names
{301: 1, 303: 2, 306: 3, 308: 4, 309: 5}

更完整的例子:

text = """301 my name is joe
303 whatsup
306 how are you doing today
308 what happened?
308 going home
309 let's go""".splitlines()

import re
names = {}
replacer = lambda x: str(names.setdefault(x.group(), len(names) + 1))
for line in text:
replaced = re.sub(r'^\d+', replacer, line)
print(replaced)

输出:

1 my name is joe
2 whatsup
3 how are you doing today
4 what happened?
4 going home
5 let's go

关于python - 如何重命名列表中的标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35458921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com