我有以下格式的列表列表:
[['Sarah', '12', 'Chocolate'],
['Anders', '11', 'Vanilla'],
['Sarah', '13', 'Strawberry'],
['John', '11', 'None'],
# ...
]
我想按如下方式对子列表进行分组:
[['Sarah', '12', 'Chocolate', '13', 'Strawberry'],
['Anders', '11', 'Vanilla'],
['John', '11', 'None'],
# ...
]
我按照子列表中的第一项进行分组,然后按第二项进行排序(因此年龄为12岁的Sarah在13岁之前来到Sarah之前)。
怎么做?
答案 0 :(得分:1)
您没有显示任何代码,因此我无法提供完整的解决方案。
一个好的数据结构是使用dict
,名称作为键,元组列表作为值:
data =[['Sarah', '12', 'Chocolate'],
['Anders', '11', 'Vanilla'],
['Sarah', '13', 'Strawberry'],
['John', '11', 'None']]
grouped = {}
for name, x, y in data:
grouped.setdefault(name, []).append((x,y))
print(grouped)
# {'Sarah': [('12', 'Chocolate'), ('13', 'Strawberry')], 'Anders': [('11', 'Vanilla')], 'John': [('11', 'None')]}
您只需要对值进行排序。
答案 1 :(得分:0)
一些可能的解决方案会浮现在脑海中,但这里只有一个。阅读评论并随意提问:
from collections import defaultdict
l = [
['Sarah', '12', 'Chocolate'],
['Anders', '11', 'Vanilla'],
['Sarah', '13', 'Strawberry'],
['John', '11', 'None'],
]
d = defaultdict(list)
for entry in l:
d[entry[0]].append(entry[1:])
# Here d looks something like this:
# {'Sarah': [['12', 'Chocolate'], ['13', 'Strawberry']], ...}
result = [
# 1. Sort the list by the first element of each sublist (parsed as an int).
# 2. Flatten the result as in https://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python/952952#952952.
# 3. Prepend the key (name).
[k] + [item for sublist in sorted(v, key=lambda x: int(x[0])) for item in sublist]
for k,v in d.items()
]
print(result)
# Output: [['Sarah', '12', 'Chocolate', '13', 'Strawberry'], ['Anders', '11', 'Vanilla'], ['John', '11', 'None']]
答案 2 :(得分:0)
你可以试试这个:
import itertools
list_1=[['Sarah', '12', 'Chocolate'],
['Anders', '11', 'Vanilla'],
['Sarah', '13', 'Strawberry'],
['John', '11', 'None']]
repeated_list=[]
unique_list=[]
for item_1,item_2 in itertools.combinations(list_1,2):
if item_1[0]==item_2[0]:
repeated_list.append(item_1+item_2)
else:
for item_3 in repeated_list:
if item_2[0] not in item_3:
if item_2 not in unique_list:
unique_list.append(item_2)
elif item_1[0] not in item_3:
if item_1 not in unique_list:
unique_list.append(item_1)
print(unique_list+repeated_list)
输出:
[['John', '11', 'None'], ['Anders', '11', 'Vanilla'], ['Sarah', '12', 'Chocolate', 'Sarah', '13', 'Strawberry']]