user3044013
user3044013

Reputation: 13

Removing items in a list

a = ['pear', 'apple?orange']

or

a = ['pear', 'apple!orange'] 

The question mark and the quotation mark can be any non alphabetic or non numeric character (<,?,<,#).

If I want to remove non alphabetic characters and make the following list:

b = ['apple', 'orange']

How do I do it? do I use a.remove or `a.split?

I'm using python 3.

Upvotes: 0

Views: 75

Answers (2)

Steve Jessop
Steve Jessop

Reputation: 279225

If you want a Unicode-aware regex to match non-alphabetic characters:

non_letters = re.compile('[\\W\\d_]', flags = re.UNICODE)
non_letters.split('apple!orange')
non_letters.split('p\xEAche0poire')

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121484

Use re.split() instead:

import re

not_letters = re.compile(r'[^a-zA-Z]')

b = not_letters.split(a[1])

Demo:

>>> import re
>>> not_letters = re.compile(r'[^a-zA-Z]')
>>> a = ['pear', 'apple?orange']
>>> not_letters.split(a[1])
['apple', 'orange']
>>> a = ['pear', 'apple!orange'] 
>>> not_letters.split(a[1])
['apple', 'orange']

Upvotes: 1

Related Questions