English 中文(简体)
制作列表列表列表一行 - python
原标题:Making list of list oneliner -python
  • 时间:2012-05-28 12:49:06
  •  标签:
  • python
  • list

我有一份名单

l=[(1,2),(1,6),(3,4),(3,6),(1,4),(4,3)]

I want to return a list that contains lists by the first number in each tuple. Something like this:

[[2,4,6],[4,6],[3]]

To make a program that iterates on list and writing a whole function that does it is easy. I want to find a oneliner - python way of doing it. Any ideas?

问题回答
>>> from itertools import groupby
>>> from operator import itemgetter
>>> L = [(1,2), (1,6), (3,4), (3,6), (1,4), (4,3)]
>>> [[y for x, y in v] for k, v in groupby(sorted(L), itemgetter(0))]
[[2, 4, 6], [4, 6], [3]]

<强 > 排除

使用 < a href=> http://docs.python.org/library/teritertools.html#itertools.groupby" rel="nofollow"\\code>retertools.groupby 。 group 在可循环的组中发现 < 强性 > concutive , 通过密钥、组配对返回循环器 。

groupby 的参数是一个关键函数, itemgetter(0) , 每一个图普都要求这个功能, 返回第一个项目作为 group 的密钥。

groupby group 中包含元素的“强度”原始顺序 。 因此,如果您想要按列表的第一个数字分组,它必须首先进行排序,这样 groupby 可以按升序排列第一个数字,并实际分组。

>>> sorted(L)
[(1, 2), (1, 4), (1, 6), (3, 4), (3, 6), (4, 3)]

有排序列表, 您可以在列表中清楚地看到如果返回最终输出将会创建的组。 现在您可以使用 < code> groupby 来显示密钥, 组配对 。

[(1, <itertools._grouper object at 0x02BB7ED0>), (3, <itertools._grouper object at 0x02BB7CF0>), (4, <itertools._grouper object at 0x02BB7E30>)]

这是按第一个数字分组的分类项目。 groupby 返回每个密钥的组为代号, 这是巨大且高效的, 但对于这个例子, 我们只需将它转换为 < code> list , 以确保它正常工作 。

>>> [(k, list(v)) for k,v in groupby(sorted(L), itemgetter(0))]
[(1, [(1, 2), (1, 4), (1, 6)]), (3, [(3, 4), (3, 6)]), (4, [(4, 3)])]

这几乎是正确的,但所要求的产出只显示每个列表中组数的第二位。 因此,以下各点可以达到预期的结果。

[[y for x, y in v] for k, v in groupby(sorted(L), itemgetter(0))]
l = [(1, 2), (1, 6), (3, 4), (3, 6), (1, 4), (4, 3)]

d = {}
for (k, v) in l:
    d.setdefault(k, []).append(v)

print d.values()

我知道这不是一个班轮, 但也许比一个班轮更容易阅读。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签