简体   繁体   中英

How to remove '\x00' from list

I would like to know, how can I delete or rather replace '\x00' in my list with ''. I have list like this

[['a', '7', '9', 'd', '4', 'e'], ['\x00', '\x00', '\x00', '\x00', 'b', 'f']]

and I want to have [['a', '7', '9', 'd', '4', 'e'], ['', '', '', '', 'b', 'f']] .

Thanks for your help

you could iterate over it and only copy strings if they are not \x00 ( l is the list)

result = []
for sub_l in l:
    new_sub_l = []
    for s in sub_l:
        if s != '\x00':
           new_sub_l.append(s)
    result.append(new_sub_l)

or use list comprehension:

result = [[s for s in sub_l if s != '\x00'] for sub_l in l]

or replace instead of delete:

result = [[s.replace('\x00', '') for s in sub_l] for sub_l in l]

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