我有一个清单:
List = [[['the'], ['cool'], ['mani'], ['Party']], [','], ['has'], ['won'], [['the'], ['Delhi'], ['elections']], [','], ['with'], [['it']], ["'s"], [['leader'], ['Arv'], ['Kejjohn']], [['leading'], ['the', 'way']], ['.']]
如果列表中有连续的元素,如果它们只有一个元素,我们如何将它们组合成一个单独的元素?
Eg:- [..., [,] , [has] , [won] , [['the'],['Delhi']] ....]
变为:
Eg:- [..., [[,],[has],[won]] , [['the'],['Delhi']] ....]
答案 0 :(得分:2)
我试图用generators
来解决这个问题def f(x):
tmp = [] # we save tmp list
for i in x:
if len(i) == 1: # if there is just one element we remember it
tmp.append(i)
else:
if tmp: # otherwise we return previously collected elements if they exist
yield tmp
tmp = [] # empty the buffer
yield i # if element have more than 1 element we just return
if tmp: yield tmp
a=[[','], ['has'], ['won'], [['the'], ['Delhi']]]
print list(f(a))
>> [[[','], ['has'], ['won']], [['the'], ['Delhi']]]
答案 1 :(得分:1)
如果您不喜欢修补缓冲区,可以使用以下解决方案。所有繁重的工作都由groupby
完成。我所要做的就是制定密钥并收集groupby
的输出。
>>> from itertools import chain, groupby
>>> from pprint import pprint
>>> result = list(chain.from_iterable(([item for item in it],) if key==1 else it for key, it in y(List, key=len)))
>>> pprint(result)
[[['the'], ['cool'], ['mani'], ['Party']],
[[','], ['has'], ['won']],
[['the'], ['Delhi'], ['elections']],
[[','], ['with'], [['it']], ["'s"]],
[['leader'], ['Arv'], ['Kejjohn']],
[['leading'], ['the', 'way']],
[['.']]]
答案 2 :(得分:0)
这是一个解决方案。我使用两个列表,一个用于结果,一个用于连接一个长度的子列表。这是非常不言自明的
l = [[['the'], ['cool'], ['mani'], ['Party']], [','], ['has'], ['won'], [['the'], ['Delhi'], ['elections']], [','], ['with'], [['it']], ["'s"], [['leader'], ['Arv'], ['Kejjohn']], [['leading'], ['the', 'way']], ['.']]
res = [] # the final result
joined = [] # a temporary list for joining together elements
for sub_list in l:
if len(sub_list) == 1: #if there's only one element
joined.append(sub_list) # put it in joined
else:
if len(joined) > 0: #if joined has some sub_lists
res.append(joined) # append them to final result
joined = [] #empty joined
res.append(sub_list) #append the current sub_list and carry on
if len( l[-1] ) == 1: #this part accounts for edge-case of a one-lengthed last element.
res.append(l[-1])
pprint(res)
输出:
[[['the'], ['cool'], ['mani'], ['Party']],
[['the'], ['Delhi'], ['elections']],
[[','], ['has'], ['won']],
[['leader'], ['Arv'], ['Kejjohn']],
[[','], ['with'], [['it']], ["'s"]],
[['leading'], ['the', 'way']],
[['.']]]