Julius Monk
Julius Monk

Reputation: 1

How to remove from list elements of the form '\xa0·\xa0' and '\xa0·\xa022h' or similar?

How to remove from list elements of the form '\xa0·\xa0' and '\xa0·\xa022h' or similar?

I have been tried the following:

from itertools import groupby 
  
# initializing list  
test_list = ['\xa0·\xa0', '\xa0·\xa022h', 'moon', 'mars', 'earth'] 
  
# initializing char list  
char_list = ['\xa'] 
  
# printing original list 
print ("The original list is : " + str(test_list)) 
  
# printing character list 
print ("The character list is : " + str(char_list)) 
  
# Remove words containing list characters 
# using loop 
res = [] 
flag = 1
for ele in test_list: 
    for idx in char_list: 
        if idx not in ele: 
            flag = 1
        else: 
            flag = 0  
            break
    if(flag == 1): 
        res.append(ele)   
  
# printing result  
print ("The filtered strings are : " + str(res)) 

credit: geeksforgeeks

Upvotes: 0

Views: 55

Answers (1)

Randy
Randy

Reputation: 14847

If you're just looking to identify elements with non-ASCII characters in it, you can you the .isascii() method of strings:

In [12]: test_list = ['\xa0\xa0', '\xa0\xa022h', 'moon', 'mars', 'earth']

In [13]: [s for s in test_list if s.isascii()]
Out[13]: ['moon', 'mars', 'earth']

Upvotes: 1

Related Questions