简体   繁体   中英

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']

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.

The condition I am using is working but for single time only

            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)

Result:

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

It's working for the first two # hash and not for the third one similarly for the comma case it working for the second value sjoshi,skekes only

NOTE: Please donot recommend re module

This may help you,

 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM