English 中文(简体)
如何统一一名独裁者的 redundant子?
原标题:How to unify redundant tuples of a dictionary?
  • 时间:2012-01-13 01:22:48
  •  标签:
  • python
t = ({ x :1}, { x :1}, { y :2})

我将统一使用:

l = []
for i in t:
     if i not in l:
        l.append(i)

tuple(l)

给出结果<代码>({x : 1},{ y : 2})

是否有更好的办法?

Another Sample Input = ({ x :1, y :1}, { x :3}, { x :1, y :2}, { x :1, y :2}) Sample output: ({ x :1, y :1}, { x :3}, { x :1, y :2})

问题回答

EDIT:对问题的答复与更新完全不同:

 dict([(x.items()[0], x) for x in t]).values()

这就需要每个字典,并把它变成一个图。 透镜是可笑的,因此可以用作字典的关键。 之后,作为关键和原始的<代码>的编码/编码”作为数值。 这意味着只有一次储存同一字典。 然后,我们把<条码>dict/条码>中的数值作为<条码>。 然后从中建立<代码>tuple。

>>> t = ({ x :1,  y :1}, { x :3}, { x :1,  y :2}, { x :1,  y :2})
>>> tuple(dict([(x.items()[0], x) for x in t]).values())
({ y : 1,  x : 1}, { x : 3}, { y : 2,  x : 1})

The following one-liner work:

dict([i.items()[0] for i in t])

我认为,这符合所有需要。 但是,一项法令不符合成员的要求,即确定现金,因此我们需要一个包裹。

class HashableDictWrapper(object):
      def __init__(self, di):
          self.di = di
          self._hash_key = id("".join(["%s=%s" % (k, di[k]) for k in sorted(di.iterkeys())]))

      def __hash__(self):
          return self._hash_key

      def __eq__(self, other):
          return self.__hash__()==other.__hash__()

if __name__=="__main__":
      t = ({ x :1}, { x :1}, { y :2})
      s = set(map(HashableDictWrapper, t))
      tuple(map(lambda a:a.di, s))

UPDATED: I make some modifications to @monkut s answer:

t = ({ x :1}, { x :1}, { y :2})
p = map(lambda di:tuple((k,di[k]) for k in sorted(di.iterkeys())), t)
result = tuple(map(lambda x:dict(x), set(p)))

这可能需要重新思考你们需要什么,但是为了这样做。

>>> t = (("x",1), ("x",1), ("y", 2))
>>> set(t)
set([( x , 1), ( y , 2)])




相关问题
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 ]="...

热门标签