gpt4 book ai didi

python - 我如何从 python 中的给定字符串创建子字符串?

转载 作者:行者123 更新时间:2023-11-28 21:56:08 26 4
gpt4 key购买 nike

您好,我如何从以下字符串创建子字符串

Netall_Low_Lin_kor_110_180
Netall_Low_Lin_cer_110_181
Netall_Low_Lin_asa_110_182
Netall_Low_Lin_row_110_183
Netall_Low_Lin_psq_182_42
Netall_Low_Lin_vyt_182_41

我想拆分上述字符串,使“Netall_Low_Lin_kor”成为一部分,“110_180”成为另一部分。对于“Netall_Low_Lin_psq_182_42”,我想将其拆分为“Netall_Low_Lin_psq”和“182_42”。

有什么办法可以拆分这些字符串吗?

最佳答案

list_of_strings = [
"Netall_Low_Lin_kor_110_180",
"Netall_Low_Lin_cer_110_181",
"Netall_Low_Lin_asa_110_182",
"Netall_Low_Lin_row_110_183",
"Netall_Low_Lin_psq_182_42",
"Netall_Low_Lin_vyt_182_41"
]

import re
pattern = re.compile("_(?=\d+_\d+)")
for current_string in list_of_strings:
print pattern.split(current_string)

输出

['Netall_Low_Lin_kor', '110_180']
['Netall_Low_Lin_cer', '110_181']
['Netall_Low_Lin_asa', '110_182']
['Netall_Low_Lin_row', '110_183']
['Netall_Low_Lin_psq', '182_42']
['Netall_Low_Lin_vyt', '182_41']

RegEx101 Demo + Explanation

关于python - 我如何从 python 中的给定字符串创建子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21970878/

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