【发布时间】:2015-04-29 10:55:34
【问题描述】:
我在 python 中有一个列表,看起来像
[['boy','121','is a male child'],['boy','121','is male'],['boy','121','is a child'],['girl','122','is a female child'],['girl','122','is a child']]
我想根据每个列表中的前 2 个条目来减少列表,得到
[['boy','121',is a male child, is male, is a child'],['girl','122','is a female child','is a child']]
有没有办法在不创建虚拟列表的情况下有效地做到这一点?
【问题讨论】:
-
我不认为列表的列表是最好的结构。为什么不使用
{('boy', 121): ['is a male child', 'is male', 'is a child'], ...}之类的东西? -
这也很好,只是我从索引搜索中获得了第一种格式,并想根据第一个 2 个条目是否匹配来减少它——如果这有意义吗?
-
是的,这是有道理的。你有没有试过什么?发生了什么?
-
我猜输出中的
is a male child必须用引号引起来