我有一个这样的数组。
a = ["]['A few hours after the end of his last match in November 2013 the Prime Ministers Office announced the decision to award him the Bharat Ratna Indias highest civilian award.', 'Did a few hours after the end of his last match in November 2013 the Prime Ministers Office annount the decision to award him the Bharat Ratna Indias highest civilian award?'], ['A few hours after the end of his last match in November 2013 the Prime Ministers Office announced the decision to award him the Bharat Ratna Indias highest civilian award.', 'When after the end of his last match in November 2013 the Prime Ministers Office announced the decision to award him the Bharat Ratna Indias highest civilian award?']"]
我想将其转换为一维列表,以便我能够从该数组中选择随机问题。
after_conversion = [['A few hours after the end of his last match in November 2013 the Prime Ministers Office announced the decision to award him the Bharat Ratna Indias highest civilian award.', 'Did a few hours after the end of his last match in November 2013 the Prime Ministers Office annount the decision to award him the Bharat Ratna Indias highest civilian award?'], ['A few hours after the end of his last match in November 2013 the Prime Ministers Office announced the decision to award him the Bharat Ratna Indias highest civilian award.', 'When after the end of his last match in November 2013 the Prime Ministers Office announced the decision to award him the Bharat Ratna Indias highest civilian award?']]
当我检查 a 的类型时,它显示了一个列表。
from itertools import chain
flatten_list = list(chain.from_iterable(z))
flatten_list
output
[']',
'[',
"'",
'A',
' ',
'f',
'e',
'w',
' ',
'h',
'o',
'u',
'r',
's',
' ',
在使用此代码时,得到这样的输出。但我想要after_conversion 中给出的输出。
你可以这样试试:
a=a[0].replace(']', '').replace('[', '').replace("'", '')
l=[]
l.append([a])
或者,按照评论中的建议使用一个衬垫:
l = [[a[0].strip("'[]")]]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句