我有一个字符串列表。根据每个字符串的长度,我需要将它们分组在一个列表中。最后一个列表应该包含所有列表。
示例:
输入
in=['the', 'way', 'you', 'see', 'people', 'is', 'the', 'way', 'you', 'treat', 'them', 'and', 'the', 'way', 'you', 'treat', 'them', 'is', 'what', 'they', 'become']
输出
expected_out=[['is'],['and', 'see', 'the', 'way', 'you'], ['them', 'they', 'what'], ['treat'], ['become', 'people']]
答案 0 :(得分:4)
不知道这是否是最好的方法,但这是我想到的第一件事:
from collections import defaultdict
len2words = defaultdict(set)
for word in input_list:
len2words[len(word)].add(word)
output = [list(len2words[key]) for key in sorted(len2words.keys())]
答案 1 :(得分:4)
您可以使用itertools.groupby
,如下所示:
from itertools import groupby
l = ['the', 'way', 'you', 'see', 'people', 'is', 'the', 'way',
'you', 'treat', 'them', 'and', 'the', 'way', 'you', 'treat',
'them', 'is', 'what', 'they', 'become']
l.sort(key=len)
output = [list(set(items)) for length, items in groupby(l, key=len)]
print(output)
<强>输出强>
[['is'], ['and', 'the', 'see', 'you', 'way'], ['them', 'what', 'they'],
['treat'], ['become', 'people']]
在对具有相同长度的连续字符串进行分组之前,这会按字符串对字符串进行排序。然后使用列表推导通过使用set
将唯一字符串解压缩到子列表中。
答案 2 :(得分:1)
我会将Selecting the number of clusters与itertools.groupby
结合使用,以免修改原始输入数据的顺序。
def change_player(self):
if self._player == 'X':
self._player = 'Y'
elif self._player == 'Y':
self._player = 'X'
else:
raise ValueError