bob90937
bob90937

Reputation: 563

edite the string python regular expression python

list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%General%%Council%%Mandate%', u'%Regional%%Council%', u'%chair%%Label%', u'%chairman%%Title%', u'%chairman%', u'%chairperson%']


list2=[u'app General Council', u'people', u'people Regional Council Mandate', u'ppp General Council Mandate', u'Regional Council', u'chair Label', u'chairman Title', u'chairman', u'chairperson']

I wanted to change list1 into list2 to remove the % replace as " ". However, when i use following statement:

print [i.replace("%"," ") for i in list1]
[u' app  General  Council ', u' people ', u' people  Regional  Council  Mandate ', u' ppp  General  Council  Mandate ', u' Regional  Council ', u' chair  Label ', u' chairman  Title ', u' chairman ', u' chairperson ']

the result is not what i want. How to remove the space in the start and end of each element? so that i can get the result which is like list2.

Upvotes: 1

Views: 50

Answers (1)

Akshay
Akshay

Reputation: 1371

You may also try replacing "%%" with " " first and "%" with "" later. That will solve your problem.

In [18]: list2 = [i.replace("%%"," ") for i in list1]

In [19]: list2
Out[19]: 
[u'%app General Council%',
 u'%people%',
 u'%people Regional Council Mandate%',
 u'%ppp General Council Mandate%',
 u'%Regional Council%',
 u'%chair Label%',
 u'%chairman Title%',
 u'%chairman%',
 u'%chairperson%']

In [20]: list2 = [i.replace("%","") for i in list2]

In [21]: list2
Out[21]: 
[u'app General Council',
 u'people',
 u'people Regional Council Mandate',
 u'ppp General Council Mandate',
 u'Regional Council',
 u'chair Label',
 u'chairman Title',
 u'chairman',
 u'chairperson']

Upvotes: 3

Related Questions