如果某些条件合并列表的元素(Combine elements of lists if some condition) python

如果满足某些条件,如何组合列表的元素。

我已经看到关于组合列表元素的帖子,但没有看到一些条件。

假设我有一个包含单词列表的列表:

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!

谢谢。

How do I combine the elements of a list if some condition is met.

I've seen posts about combining elements of a list, but not with some condition.

Say I have a list containing lists of words:

words = [ ['this','that!','riff','raff'], ['hip','hop!','flip!','flop'], ['humpty','dumpty!','professor!','grumpy!'] ]

How do I combine only those elements that contain an !?

For example, the output would look like this:

[['this', 'that!', 'riff', 'raff'], ['hip', 'hop!, flip!', 'flop'], # 1,2 are now combined ['humpty', 'dumpty!, professor!, grumpy!']] # 1,2,3 are now combined

I tried this:

for word in words: word = ', '.join(i for i in word if re.search('!',str(i))) print word

but got:

that! hop!, flip! dumpty!, professor!, grumpy!

Thank you.

最满意答案

使用itertools.groupby :

>>> 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!']]

Use itertools.groupby:

>>> 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!']]如果某些条件合并列表的元素(Combine elements of lists if some condition) python

如果满足某些条件,如何组合列表的元素。

我已经看到关于组合列表元素的帖子,但没有看到一些条件。

假设我有一个包含单词列表的列表:

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!

谢谢。

How do I combine the elements of a list if some condition is met.

I've seen posts about combining elements of a list, but not with some condition.

Say I have a list containing lists of words:

words = [ ['this','that!','riff','raff'], ['hip','hop!','flip!','flop'], ['humpty','dumpty!','professor!','grumpy!'] ]

How do I combine only those elements that contain an !?

For example, the output would look like this:

[['this', 'that!', 'riff', 'raff'], ['hip', 'hop!, flip!', 'flop'], # 1,2 are now combined ['humpty', 'dumpty!, professor!, grumpy!']] # 1,2,3 are now combined

I tried this:

for word in words: word = ', '.join(i for i in word if re.search('!',str(i))) print word

but got:

that! hop!, flip! dumpty!, professor!, grumpy!

Thank you.

最满意答案

使用itertools.groupby :

>>> 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!']]

Use itertools.groupby:

>>> 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!']]