如何将二维数组转换为一维数组?

罗什尼·希拉尼

我有一个这样的数组。

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将一维数组转换为二维数组?

如何将二维数组转换为Set?

如何方便地将二维数组转换为二维向量?

将二维数组转换为一维数组,交替使用它们的值

使用DAG对象将一维数组转换为二维数组

如何将二维数组转换为一维数组?

如何将数据从一维数组转换为二维对象数组并更改某些元素?

如何将类似数据的数组从字符串转换为二维数组?

将一维数组转换为二维数组

如何将多维数组转换为二维数组?

将一维数组转换为9列的二维数组

PHP数组从二维转换为一维

将二维数组转换为单维数组

如何将数组列表转换为二维数组?

如何将多维数组转换为二维数组?

JavaScript:将一维数组按给定大小转换为二维数组

将一维数组转换为二维数组

将一维数组转换为二维数组

如何将二维字符串数组转换为一维字符串数组?

如何将多维数组转换为二维数组

如何将二维嵌套数组转换为二维数组单个?

将一维数组转换为二维数组

Matplotlib plot_surface:如何将一维数组转换为所需的二维输入?

如何将数组([数组([])] 转换为二维 numpy 数组?

如何将字典中的二维数组转换为一个数组?

Pandas - 将一列一维数组转换为二维数组

Pandas - 将一列一维数组转换为二维数组

将一维数组转换为索引值的二维向量数组的 Pythonic/Numpy 方法

python将一维概率数组转换为二维数组