English 中文(简体)
我是否应当使用字典或清单?
原标题:Should I use dict or list?

我谨提出两个大层面的清单:

authors = [["Bob", "Lisa"], ["Alice", "Bob"], ["Molly", "Jim"], ... ]

并编制一份清单,列出作者的所有姓名。

当我通过名单时,我需要一个集装箱储存已经看到的我的名字。

清单:

seen = []
for author_list in authors:
    for author in author_list:
        if not author in seen:
            seen.append(author)
result = seen

seen = {}
for author_list in authors:
    for author in author_list:
        if not author in seen:
            seen[author] = True
result = seen.keys()

越快? 还是更好的解决办法?

最佳回答

You really want a set. Sets are faster than lists because they can only contain unique elements, which allows them to be implemented as hash tables. Hash tables allow membership testing (if element in my_set) in O(1) time. This contrasts with lists, where the only way to check if an element is in the list is to check every element of the list in turn (in O(n) time.)

。 两者均允许<代码>O(1) 成员测试。 差异是,<条码>只有钥匙,而<条码>则有条码>。 具有关键和价值(在申请中不需要额外的间接费用)。


Using a set, and replacing the nested for loop with an itertools.chain() to flatten the 2D list to a 1D list:

import itertools
seen = set()
for author in itertools.chain(*authors):
    seen.add(author)

Or shorter:

import itertools
seen = set( itertools.chain(*authors) )

Edit (thanks, @jamylak)

import itertools
seen = set( itertools.chain.from_iterable(authors) )

名单例:

>>> a = [[1,2],[1,2],[1,2],[3,4]]
>>> set ( itertools.chain(*a) )
set([1, 2, 3, 4])

P.S.:如果你不想找到所有独特的作者,就希望。 页: 1 反,特殊种类的字典选择计算。

这里的例子有:

>>> a = "DEADBEEF CAFEBABE"
>>> import collections
>>> collections.Counter(a)
Counter({ E : 5,  A : 3,  B : 3,  D : 2,  F : 2,    : 1,  C : 1})
问题回答

www.un.org/Depts/DGACM/index_french.htm

>>> authors = [["Bob", "Lisa"], ["Alice", "Bob"], ["Molly", "Jim"]]
>>> from itertools import chain
>>> set(chain(*authors))
set([ Lisa ,  Bob ,  Jim ,  Molly ,  Alice ])

上乘,然后使用<>code><>。

import itertools
result = set(itertools.chain.from_iterable(authors))

您可以使用:

from sets import Set

seen = Set()

for author_list in authors:
    for author in author_list:
        seen.add(author)

result = seen

这样,你就会放弃“如果”检查,因此解决办法将会更快。

If you care about the performance of lookups, lookups in lists are O(n), while lookups in dictionaries are amortised to O(1).

https://stackoverflow.com/questions/513882/python-list-vs-dict-for-look-up-table>here。

Lists just store a bunch of items in a particular order. Think of your list of authors as a long line of pigeonhole boxes with author s names on bits of papers in the boxes. The names stay in the order you put them in, and you can find the author in any particular pigeonhole very easily, but if you want to know if a particular author is in any pigeonhole, then you have to look through each one until you find the name you re after. You can also have the same name in any number of pigeonholes.

假肢更像一个电话簿。 鉴于提交人的名字,你可以迅速检查,看看看提交人是否被列入电话簿,并找到电话号码。 但是,你只能把每个作者都包括一次(每打一个电话号码),而且你可以命令提交人到那里,他们必须按电话簿的意义行事。 在一本真正的电话书中,该命令是字母;在斯堪的字典中,该命令完全无法预测(当你在字典上添加或删除物品时,该命令可以比在电话簿上更快地找到文字。

而电话则与电话号码一样,即:www.em.int/em/ list name,而不是电话号码。 你仍然可以把同一名列一.,或者说不是这样。 而你仍然可以命令把名单列入其中,以作任何用途。 但是,你可以非常迅速检查一个名词是否属于该名。


鉴于你的使用情况,一套选择似乎是显而易见的。 你不关心你发现作者的顺序,或你多次看到每个作者,只有你能够迅速检查你是否看到某个作者。

案件清单是坏的;无论按照你具体指明的顺序,他们都努力记住重复,而且搜寻工作进展缓慢。 但是,你也没有必要绘制价值观的关键图,而这正是dict。 为了回到电话簿类比,你没有相当于“电话号码”的任何东西;在你的字典例子中,你重新撰写了一本电话书,其中每个人的号码都被列为<代码>True,因此,两人都把电话号码列入名单?

一套,即德国统计局,确实是你所需要的。





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

热门标签