2024-09-25 00:25:18 发布
网友
我有一个如下列表:
test = ['bla bla','affect /affected / affecting'] print(test)
我想在列表中创建新的元素,通过分隔一个元素,但通过“/”符号分隔
例如,这种情况下的期望输出:
test_new = ['bla bla','affect','affected','affecting']
我该怎么做
编辑:列表可以有多个元素,但并非所有元素都有“/”符号
您可以使用列表理解和'/'和strip上的split来删除空格(假设您有多个字符串,作为一个列表):
'/'
strip
split
[j.strip() for i in test for j in i.split('/')] # ['affect', 'affected', 'affecting']
testEntry=test[0] testEntry=test.replace(' ','') test=testEntry.split('/')
您可以使用列表理解和
'/'
和strip
上的split
来删除空格(假设您有多个字符串,作为一个列表):相关问题 更多 >
编程相关推荐