列表

时间:2018-03-08 00:52:30

标签: python

我有一个具有以下结构的列表:

input =
[['Business']
 ['Technology']
 ['Technology Business Sports']]

我想将句子分成单词并创建以下结构:

result =
[['Business']
 ['Technology']
 ['Technology', 'Business', 'Sports']]

我试过这个

result = input
for i, s in enumerate(input):
    result[i] = s[0].split()
print(result)

但我收到错误:

  

ValueError:无法将大小为2的序列复制到数组轴   维度1

2 个答案:

答案 0 :(得分:2)

你在找这样的东西吗?

input = [['Business'], ['Technology'], ['Technology Business Sports']]
output = []
for elem in input:
    output.append(elem[0].split(" "))

输出:

[['Business'], ['Technology'], ['Technology', 'Business', 'Sports']]

答案 1 :(得分:2)

你想要这样的东西:

>>> data = [['Business'],
...  ['Technology'],
...  ['Technology Business Sports']]
>>> 
>>> [inner[0].split() for inner in data]
[['Business'], ['Technology'], ['Technology', 'Business', 'Sports']]