[英]Grouping Nested Lists into Tuples
I am trying to group nested lists in python (I already sorted the data).我正在尝试在 python 中对嵌套列表进行分组(我已经对数据进行了排序)。 The final output should be comma separated tuples, one per line
最终输出应该是逗号分隔的元组,每行一个
Current nested list:当前嵌套列表:
[['A123', '2012-01-01', 'estrogen'],
['A123', '2012-01-01', 'ibuprofen '],
['A123', '2014-01-01', 'oxaliplatin'],
['A123', '2014-01-01', 'penicilin']]
Desired outcome (comma separated tuple)期望的结果(逗号分隔的元组)
[(('A123', '2012-01-01', 'estrogen'),
('A123', '2012-01-01', 'ibuprofen ')),
(('A123', '2014-01-01', 'oxaliplatin'),
('A123', '2014-01-01', 'penicilin'))]
I understand the sorting but don't quite know how to group the lists together into one.我了解排序,但不太知道如何将列表组合为一个。 The final step I assume is the convert into tuples.
我假设的最后一步是转换为元组。
Since you already sorted your data, you can convert the lists to tuples and create a tuple (couple) of tuples 2 by 2:由于您已经对数据进行了排序,因此您可以将列表转换为元组并创建一个 2 x 2 的元组(一对):
l = [['A123', '2012-01-01', 'estrogen'],['A123', '2012-01-01', 'ibuprofen'],['A123', '2014-01-01', 'oxaliplatin'],
['A123', '2014-01-01', 'penicilin']]
result = [(tuple(l[i]),tuple(l[i+1])) for i in range(0,len(l),2)]
print(result)
variant using zip
and sliced sub-lists:使用
zip
和切片子列表的变体:
result = [(tuple(u),tuple(v)) for u,v in zip(l[::2],l[1::2])]
you don't control the way the data is printed unless you use pprint
module.除非您使用
pprint
模块,否则您无法控制数据的打印方式。
import pprint
pprint.pprint(result,width = 50)
Since I chose a width of 50, I get one element per line (it depends on the data, but it seems that it provides exactly the output mentioned in your question, horizontal alignment included)由于我选择了 50 的宽度,因此每行得到一个元素(这取决于数据,但它似乎提供了您问题中提到的输出,包括水平对齐)
[(('A123', '2012-01-01', 'estrogen'),
('A123', '2012-01-01', 'ibuprofen')),
(('A123', '2014-01-01', 'oxaliplatin'),
('A123', '2014-01-01', 'penicilin'))]
This looks like a case for itertools.groupby
:这看起来像是
itertools.groupby
一个案例:
>>> data = [['A123', '2012-01-01', 'estrogen'],
... ['A123', '2012-01-01', 'ibuprofen '],
... ['A123', '2014-01-01', 'oxaliplatin'],
... ['A123', '2014-01-01', 'penicilin']]
>>> [tuple(grp) for key, grp in itertools.groupby(data, key=lambda x: x[:2])]
[(['A123', '2012-01-01', 'estrogen'], ['A123', '2012-01-01', 'ibuprofen ']),
(['A123', '2014-01-01', 'oxaliplatin'], ['A123', '2014-01-01', 'penicilin'])]
This groups all items that have the same first two elements ( key=lambda x: x[:2]
) together in one tuple.这将具有相同前两个元素 (
key=lambda x: x[:2]
) 的所有项目组合在一个元组中。 Note that the data has to be sorted by the key for this to work, but according to you this is already the case.请注意,必须按键对数据进行排序才能使其工作,但据您称,情况已经如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.