x =['Jeff Bezos (chairman', ' president and CEO)', 'Werner Vogels (CTO)', '']
x ="".join(x)我希望去掉括号内的引号,然后退出
x =['Jeff Bezos (chairman,president and CEO)', 'Werner Vogels (CTO)', '']发布于 2019-01-25 13:00:52
您的示例输入太小,无法创建一个可信的模式,但假设您希望在遇到右大括号时加入列表元素,您可以做一些简单的事情,如:
x = ['Jeff Bezos (chairman', ' president and CEO)', 'Werner Vogels (CTO)', '']
y = ['']
for element in x:
y[-1] += element
if element.endswith(')'):
y.append('')
print(y)
# ['Jeff Bezos (chairman president and CEO)', 'Werner Vogels (CTO)', '']但要真正基于左大括号进行连续连接,并为每个连接添加逗号,您需要记录左大括号的前一个状态,然后等待结束状态,然后使用逗号将所有内容连接在一起-类似于:
x = ['Jeff Bezos (chairman', ' president and CEO)', 'Werner Vogels (CTO)', '']
y = []
cont = False
for element in x:
if not cont:
y.append('')
y[-1] += (',' if cont else '') + element
cont = element.rfind('(') > element.rfind(')')
print(y)
# ['Jeff Bezos (chairman, president and CEO)', 'Werner Vogels (CTO)', '']请注意,如果在同一元素中有多个大括号,即使这样也容易导致糟糕的连接,但问题是您愿意深入到多深以确保覆盖每个边缘情况。
发布于 2019-01-25 14:51:58
import re
x = ['Jeff Bezos (chairman', ' president and CEO)', 'Werner Vogels (CTO)', '']
m=re.search('(\(.*?\))', str(x)) #finding the pattern starting with'('ending with')'
srt=m.group(0).replace("'", "") # replacing single quotes within brackets.
x = re.sub('(\(.*?\))', srt, str(x),1) #replacing the updated string in list
print '\n',x输出:
['Jeff Bezos (chairman, president and CEO)', 'Werner Vogels (CTO)', '']https://stackoverflow.com/questions/54358932
复制相似问题