Python 删除两个单词之间的连字符(-)会在列表中创建空元素

Python 删除两个单词之间的连字符(-)会在列表中创建空元素,python,python-3.x,string,list,Python,Python 3.x,String,List,我试图通过下面的代码去除列表中单词中的连字符 listA=['End-to-End Encryption'] listmain= '-'.join(listA).split('-') 我得到的输出是 ['End', '', '', 'to', '', '', 'End', 'Encryption'] 如何消除创建的不必要的空白元素 理想输出要求 ['End', 'to', 'End', 'Encryption'] 如何实现这一点。用多个分隔符将其拆分。由正则表达式生成 listA

我试图通过下面的代码去除列表中单词中的连字符


listA=['End-to-End Encryption']

listmain= '-'.join(listA).split('-')

我得到的输出是

['End', '', '', 'to', '', '', 'End', 'Encryption']

如何消除创建的不必要的空白元素

理想输出要求

['End', 'to', 'End', 'Encryption']

如何实现这一点。

用多个分隔符将其拆分。由正则表达式生成

listA = ['End-to-End Encryption']
listmain = re.split(r'\s|-', listA[0])

这应该可以完成任务,并且对于listA中发送的1个以上是动态的

listmain = [val.replace("-"," ").split() for val in listA]
例如:

O/p:


以下代码可以完美地工作:

listA=['End-to-End Encryption']
for el in listA:
    str2 = el.split('-')
    print(str2)
此代码的输出为:

['End', 'to', 'End Encryption']

listmain=listA[0].replace(“,-”).split(“-”)
我认为列表不允许空白元素,并且您的代码对我有效—没有空元素。我已经运行了您的代码,并且我的输出与您的不同。您的python版本是什么?@I'L'I此解决方案有效。str_list=过滤器(无,str_list)。谢谢
listA=['End-to-End Encryption']
for el in listA:
    str2 = el.split('-')
    print(str2)
['End', 'to', 'End Encryption']