3 回答
TA贡献1936条经验 获得超6个赞
重要说明:
groups = []uniquekeys = []for k, g in groupby(data, keyfunc): groups.append(list(g)) # Store group iterator as a list uniquekeys.append(k)
k
g
groupby
from itertools import groupby things = [("animal", "bear"), ("animal", "duck"), ("plant", "cactus"), ("vehicle", "speed boat"), ("vehicle", "school bus")]for key, group in groupby(things, lambda x: x[0]): for thing in group: print "A %s is a %s." % (thing[1], key) print " "
熊是动物。 鸭子是动物。
仙人掌是一种植物。
快艇是一种交通工具。 校车就是一辆车。
things
groupby()
lambda x: x[0]
groupby()
for
groupby
for key, group in groupby(things, lambda x: x[0]): listOfThings = " and ".join([thing[1] for thing in group]) print key + "s: " + listOfThings + "."
动物:熊和鸭子。 植物:仙人掌。 车辆:快艇和校车。
TA贡献1963条经验 获得超6个赞
groups = []uniquekeys = []for k, g in groupby(data, keyfunc): groups.append(list(g)) # Store group iterator as a list uniquekeys.append(k)
groupby()
groupby
groupby
TA贡献1815条经验 获得超6个赞
[(c,len(list(cgen))) for c,cgen in groupby(some_string)]
itertools.groupby
GROUP BY
添加回答
举报