gpt4 book ai didi

python - 编辑字符串 python 正则表达式 python

转载 作者:行者123 更新时间:2023-11-30 22:48:06 26 4
gpt4 key购买 nike

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

我想将 list1 更改为 list2 以删除 % 替换为“”。但是,当我使用以下语句时:

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

结果不是我想要的。如何删除每个元素开头和结尾的空格?这样我就可以得到类似于list2的结果。

最佳答案

您也可以尝试先将 "%%" 替换为 "",然后将 "%" 替换为 "" > 稍后。这将解决您的问题。

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

关于python - 编辑字符串 python 正则表达式 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40297929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com