如果满足某些条件,如何组合列表的元素。
我看过关于组合列表元素的帖子,但没有一些条件。
假设我有一个包含单词列表的列表:
words = [
['this','that!','riff','raff'],
['hip','hop!','flip!','flop'],
['humpty','dumpty!','professor!','grumpy!']
]
如何仅合并包含!
?
例如,输出看起来像这样:
[['this', 'that!', 'riff', 'raff'],
['hip', 'hop!, flip!', 'flop'], # 1,2 are now combined
['humpty', 'dumpty!, professor!, grumpy!']] # 1,2,3 are now combined
我试过了:
for word in words:
word = ', '.join(i for i in word if re.search('!',str(i)))
print word
但得到了:
that!
hop!, flip!
dumpty!, professor!, grumpy!
谢谢。
答案 0 :(得分:5)
>>> from itertools import groupby
>>> out = []
>>> for lst in words:
d = []
for k, g in groupby(lst, lambda x: '!' in x):
if k:
d.append(', '.join(g))
else:
d.extend(g)
out.append(d)
...
>>> out
[['this', 'that!', 'riff', 'raff'],
['hip', 'hop!, flip!', 'flop'],
['humpty', 'dumpty!, professor!, grumpy!']]
答案 1 :(得分:3)
这是我的解决方案:
words = [
['this','that!','riff','raff'],
['hip','hop!','flip!','flop'],
['humpty','dumpty!','professor!','grumpy!']
]
output = []
for wl in words:
out_wl = []
bang_wl = []
for w in wl:
if '!' in w:
bang_wl.append(w)
else:
if bang_wl:
out_wl.append(','.join(bang_wl))
bang_wl = []
out_wl.append(w)
if bang_wl:
out_wl.append(','.join(bang_wl))
output.append(out_wl)
print output
[['this', 'that!', 'riff', 'raff'], ['hip', 'hop!,flip!', 'flop'], ['humpty', 'dumpty!,professor!,grumpy!']]
bang_wl
累积!
字词,直至找到不包含!
的字词。此时,它join
中的bang_wl
中的单词会附加到output_wl
列表中。
答案 2 :(得分:1)
result = []
for sub_lst in words:
result.append([])
temp = ""
for ele in sub_lst:
if not temp and not "!" in ele:
result[-1].append(ele)
elif temp and not "!" in ele:
result[-1].append(temp)
result[-1].append(ele)
temp = ""
else:
temp += "," + ele if temp else ele
if temp:
result[-1].append(temp)
[['this', 'that!', 'riff', 'raff'], ['humpty', 'dumpty!,professor!,grumpy!'], ['hip', 'hop!,flip!', 'flop']]
如果您希望加入包含!
的所有字词,包括由不包含!
的字词分隔的字词,即['humpty', 'dumpty!', 'professor!', 'grumpy!',"foo","bar!"]
将成为
['humpty', 'foo', 'dumpty!,professor!,grumpy!,bar!']
:
result = []
for sub_l in words:
result.append([])
temp = ""
for word in sub_l:
if "!" in word:
temp += "," + word if temp else word
else:
result[-1].append(word)
result[-1].append(temp)
有些时间显示@vikramls效率最高,而itertools解决方案效率最低。
In [31]: %%timeit
....: result = []
....: for sub_lst in words:
....: result.append([])
....: temp = ""
....: for ele in sub_lst:
....: if not temp and not "!" in ele:
....: result[-1].append(ele)
....: elif temp and not "!" in ele:
....: result[-1].append(temp)
....: result[-1].append(ele)
....: temp = ""
....: else:
....: temp += "," + ele if temp else ele
....: if temp:
....: result[-1].append(temp)
....:
100000 loops, best of 3: 16 µs per loop
In [32]: %%timeit
output = []
for wl in words:
out_wl = []
bang_wl = []
for w in wl:
if '!' in w:
bang_wl.append(w)
else:
if bang_wl:
out_wl.append(','.join(bang_wl))
bang_wl = []
out_wl.append(w)
if bang_wl:
out_wl.append(','.join(bang_wl))
output.append(out_wl)
....:
100000 loops, best of 3: 15.2 µs per loop
In [33]: %%timeit
out = []
>>> for lst in words:
d = []
for k, g in groupby(lst, lambda x: '!' in x):
if k:
d.append(', '.join(g))
else:
d.extend(g)
out.append(d)
....:
10000 loops, best of 3: 48.1 µs per loop
如果您只想要以!
结尾的字词:
In [34]: %%timeit
result = []
for sub_lst in words:
result.append([])
temp = ""
for ele in sub_lst:
if not temp and not ele[-1] == "!":
result[-1].append(ele)
elif temp and not ele[-1] == "!":
result[-1].append(temp)
result[-1].append(ele)
temp = ""
else:
temp += "," + ele if temp else ele
if temp:
result[-1].append(temp)
....:
100000 loops, best of 3: 17 µs per loop