2 回答
TA贡献1809条经验 获得超8个赞
您的问题非常无效,但是,
这对你有用吗:
names = 'test_1_1', 'test_1_2', 'test_1_3-4-5'
res = []
for name in names:
if '-' not in name:
res.append(name)
continue
parts = name.split('_')
for sub in parts[2].split('-'):
res.append(f'{parts[0]}_{parts[1]}_{sub}') # This is what makes sense to me but maybe you want the following:
# res.append(f'{parts[0]},{parts[1]},{sub}')
print(res)
输出:
['test_1_1', 'test_1_2', 'test_1_3', 'test_1_4', 'test_1_5']
TA贡献1810条经验 获得超4个赞
假设您string确实是 a tuple,您可以使用:
import re
name = 'test_1_1', 'test_1_2', 'test_1_3-4-5'
new_name = [re.split(r"[_-]", x) for x in name]
# [['test', '1', '1'], ['test', '1', '2'], ['test', '1', '3', '4', '5']]
或者,由于我没有完全理解您的问题,您可能需要:
new_name = [re.sub(r"[_-]", ",", x) for x in name ]
# ['test,1,1', 'test,1,2', 'test,1,3,4,5']
添加回答
举报