![](/img/trans.png)
[英]How to remove special character in a list of string and split it into separate elements
[英]Trying to remove the list if special character in string not working for all
['#', 'vcrisan', '#sses', '#crusu', 'ALL', '#rpavlicek', 'oracle', '#vcrisan', 'dwilks,skumar', 'sjoshi,skekes', 'skekes', 'sdammalapati', 'sdammalapati']
我正在尝试使用列表中的#删除字符串,如果该字符串像'dwilks,skumar'将会再次将其拆分并再次添加到字符串中,从而删除旧的字符串。
我正在使用的条件有效,但仅适用于一次
for name in userslist:
if '#' in name:
userslist.remove(name)
if ',' in name:
newwlist=name.split(',')
userslist.remove(name)
for splittedname in newwlist:
userslist.append(splittedname)
print (userslist)
结果:
['vcrisan','#crusu','ALL','oracle','dwilks,skumar','skekes','sdammalapati','sdammalapati','sjoshi','skekes']
它适用于前两个#哈希,不适用于第三个#哈希,仅适用于第二个值sjoshi
注意:请不要建议重新组装
这可能对您有帮助,
userslist = ['#', 'vcrisan', '#sses', '#crusu', 'ALL', '#rpavlicek', 'oracle', '#vcrisan', 'dwilks,skumar', 'sjoshi,skekes', 'skekes', 'sdammalapati', 'sdammalapati']
pUserList = []
for name in userslist:
if not name.startswith('#'):
pUserList.extend(name.split(','))
print (pUserList)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.