How do you remove exactly one complete namespace prefix from a sample ID?

removeprefix removes one full prefix only when the string begins with that literal text.

Python example and interpretation

Removing one train: from train:train:17 leaves train:17. This preserves repeated text that belongs to the original identifier.

[s.removeprefix('train:') for s in ['train:train:17','test:17']]

Result ['train:17', 'test:17']

中文

样本编号怎样只删除一个完整的命名空间前缀?

removeprefix 仅在字符串从指定文本开始时删除这一个完整前缀,保留其他字符。

Python 算例与适用条件

train:train:17 删除一次后仍有 train:17。若尾部是用户提供的原始标识,这种行为可避免误删重复片段。

[s.removeprefix('train:') for s in ['train:train:17','test:17']]

结果 ['train:17', 'test:17']

References / 参考来源

Keep reading / 继续阅读