[英]How to group elements of a list?
I have a list of lists.我有一个列表列表。 Each list contains three elements, call them
[number, letter, time]
.每个列表包含三个元素,称它们为
[number, letter, time]
。 It looks like this:它看起来像这样:
my_list = [[0, 'a', '0:00'],
[1, 'a', '1:00'],
[2, 'b', '2:00'],
[3, 'c', '3:00'],
[4, 'b', '4:00'],
[5, 'a', '5:00'],
[6, 'd', '6:00'],
[7, 'c', '7:00'],
[8, 'b', '8:00'],
[9, 'e', '9:00']]
I want to format this list such that element[1]
, the letter
of each nested list becomes element[0]
of a new nested list, with the occurrences of number
and time
as element[1]
and element[2]
.我想格式化这个列表,使得
element[1]
,每个嵌套列表的letter
成为新嵌套列表的element[0]
, number
和time
的出现为element[1]
和element[2]
。
The output I want should look like this我要的output应该是这样的
my list = [['a', [0, 1, 5], ['0:00', '1:00', '5:00']],
['b', [2, 4, 8], ['2:00', '4:00', '8:00']],
['c', [3, 7], ['3:00', '7:00']],
['d', [6], ['6:00']],
['e', [9], ['9:00']]]
So far I have used collections.Counter()
to get the frequency of letter
到目前为止,我已经使用
collections.Counter()
来获取letter
的频率
>>> letter_count = Counter([element[1] for element in my_list])
>>> letter_count
Counter({'a': 3, 'b': 3, 'c': 2, 'd': 1, 'e': 1})
I believe I can use a list comprehension to get the rest of the way, using letter_count.keys()
and a conditional, but I'm stuck.我相信我可以使用列表理解来获得 rest,使用
letter_count.keys()
和条件,但我被卡住了。 Any suggestions?有什么建议么?
Also, any suggestions on a better, more descriptive title (for posterity's sake) are welcome.此外,欢迎就更好、更具描述性的标题提出任何建议(为了后代)。
out = {}
for a, b, c in my_list:
out.setdefault(b, []).append((a, c))
out = [[k, [i for i, _ in v], [i for _, i in v]] for k, v in out.items()]
print(out)
Prints:印刷:
[['a', [0, 1, 5], ['0:00', '1:00', '5:00']],
['b', [2, 4, 8], ['2:00', '4:00', '8:00']],
['c', [3, 7], ['3:00', '7:00']],
['d', [6], ['6:00']],
['e', [9], ['9:00']]]
Another approach is to use itertools.groupby
另一种方法是使用
itertools.groupby
import itertools
new_list = []
my_list = sorted(my_list, key=lambda row: row[1])
for name, groups in itertools.groupby(my_list, key=lambda row: row[1]):
groups = list(zip(*groups))
grouped_item = [name, groups[0], groups[2]]
new_list.append(grouped_item)
print(new_list)
[['a', (0, 1, 5), ('0:00', '1:00', '5:00')],
['b', (2, 4, 8), ('2:00', '4:00', '8:00')],
['c', (3, 7), ('3:00', '7:00')],
['d', (6,), ('6:00',)],
['e', (9,), ('9:00',)]]
Here's a quick n' dirty approach (same efficiency as current other answers, but more code)这是一种快速而肮脏的方法(与当前其他答案的效率相同,但代码更多)
my_list = [[0, 'a', '0:00'],
[1, 'a', '1:00'],
[2, 'b', '2:00'],
[3, 'c', '3:00'],
[4, 'b', '4:00'],
[5, 'a', '5:00'],
[6, 'd', '6:00'],
[7, 'c', '7:00'],
[8, 'b', '8:00'],
[9, 'e', '9:00']]
tmp_dict = {}
for first, second, third in my_list:
if second not in tmp_dict:
tmp_dict[second] = [[],[]]
tmp_dict[second][0].append(first)
tmp_dict[second][1].append(third)
new_list = []
for key, val in tmp_dict.items():
new_list.append([key] + val)
print(new_list)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.