a =“[['CC','JJ'],['CC','NN'],['CC','NNS'],['DT','IN'],['DT ”, 'NN'],['DT','NNS'],['DT','RB'],['DT','VBZ'],['IN','IN'], ['IN','JJ'],['IN','NN'],['IN','NNS'],['IN','PRP'],['IN', 'RB'],['IN','VBG'],['IN','VBN'],['IN','VBZ'],['JJ','DT'], ['JJ','IN'],['JJ','RB'],['NN','CC'],['NN','DT'],['NN','IN'], ['NN','JJ'],['NN','NN'],['NN','PRP $'],['NN','VBG'],['NN', 'VBN',['NN','VBZ'],['NNS','DT'],['NNS','IN'],['NNS','JJ'], ['NNS','NN'],['PRP','CC'],['PRP','IN'],['PRP','RB'],['PRP', 'VB'],['PRP','VBD'],['PRP','VBP'],['RB','CC'],['RB','IN'], ['RB','NN'],['RB','PRP'],['RB','RB'],['RB','VBD'],['RB', 'VBP'],['RB','VBZ'],['TO','NN'],['VB','RB'],['VB','TO'], ['VBD','NN'],['VBD','PRP'],['VBD','RB'],['VBG','IN'],['VBG', 'NN'],['VBG','RB'],['VBN','RB'],['VBP','NNS'],['VBP','PRP'], ['VBP','RB'],['VBP','TO'],['VBZ','NN'],['VBZ','PRP']]“
a 是字符串如何将 a 转换为以下格式
['CC JJ', 'CC NN', 'CC NNS','DT IN','DT NN', 'DT RB',etccc]
答案 0 :(得分:6)
使用ast.literal_eval
和列表理解:
>>> from ast import literal_eval
>>> a = "[['CC', 'JJ'], ['CC', 'NN'], ['CC', 'NNS'], ['DT', 'IN'], ['DT', 'NN']]"
>>> [' '.join(x) for x in literal_eval(a)]
['CC JJ', 'CC NN', 'CC NNS', 'DT IN', 'DT NN']
这里ast.literal_eval
将包含列表的字符串(安全地)转换为Python列表,然后我们可以对列表中的项目应用str.join
。