如果字符串中的特殊字符不起作用,尝试删除列表

2024-09-30 05:24:16 发布

您现在位置:Python中文网/ 问答频道 /正文

['#', 'vcrisan', '#sses', '#crusu', 'ALL', '#rpavlicek', 'oracle', '#vcrisan', 'dwilks,skumar', 'sjoshi,skekes', 'skekes', 'sdammalapati', 'sdammalapati']

I am trying to remove string with # in the list and if the string is like 'dwilks,skumar' will split it again and add to the string again removing the old one.

我使用的条件是工作,但只有一次

            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'、'sdamalapati'、'sdamalapati'、'sjoshi'、'skekes']

它适用于前两个#散列,而不适用于第三个#散列同样适用于逗号大小写它适用于第二个值sjoshi,skekes only

注意: 请不要推荐re模块


Tags: thenameinstringifallremoveoracle
1条回答
网友
1楼 · 发布于 2024-09-30 05:24:16

这也许对你有帮助

 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)

相关问题 更多 >

    热门问题