jeudi 15 mai 2014

convertir une chaîne en un format à l'aide de python - Stack Overflow



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 is string how can i convert a as following form


['CC  JJ', 'CC  NN', 'CC  NNS','DT  IN','DT  NN', 'DT  RB',etccc]



Use ast.literal_eval and a list comprehension:


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

Here ast.literal_eval converts(safely) the string containing the list into a Python list, and then we can apply str.join on the items of the list.




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 is string how can i convert a as following form


['CC  JJ', 'CC  NN', 'CC  NNS','DT  IN','DT  NN', 'DT  RB',etccc]


Use ast.literal_eval and a list comprehension:


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

Here ast.literal_eval converts(safely) the string containing the list into a Python list, and then we can apply str.join on the items of the list.


0 commentaires:

Enregistrer un commentaire