在Python中,什么时候使用字典,列表或集合?

问题:在Python中,什么时候使用字典,列表或集合?

我什么时候应该使用字典,列表或集合?

是否存在更适合每种数据类型的方案?

When should I use a dictionary, list or set?

Are there scenarios that are more suited for each data type?


回答 0

一个list保持秩序,dictset不要:当你关心的秩序,因此,您必须使用list(如果你的容器的选择仅限于这三种,当然;-)。

dict与每个键关联一个值,而listset仅包含值:很明显,非常不同的用例。

set要求项目是可哈希的,list不是:如果您有不可哈希的项目,则不能使用,set而必须使用list

set禁止重复,list不禁止:也是至关重要的区别。(可以在以下位置找到“多重集”,该多重集将重复项映射到不止一次存在的项目的不同计数中;如果出于某些奇怪的原因而无法导入,则collections.Counter可以将其构建为,或者在2.7之前的版本中Python作为,使用项目作为键,并将相关值作为计数)。dictcollectionscollections.defaultdict(int)

set(或dict键中)中检查值的隶属关系非常快(花费一个恒定的短时间),而在列表中,它花费的时间与列表的长度成正比(在一般情况下和最坏情况下)。因此,如果您有可散列的项目,则不关心订单或重复项,而希望快速进行成员资格检查set比更好list

A list keeps order, dict and set don’t: when you care about order, therefore, you must use list (if your choice of containers is limited to these three, of course;-).

dict associates with each key a value, while list and set just contain values: very different use cases, obviously.

set requires items to be hashable, list doesn’t: if you have non-hashable items, therefore, you cannot use set and must instead use list.

set forbids duplicates, list does not: also a crucial distinction. (A “multiset”, which maps duplicates into a different count for items present more than once, can be found in collections.Counter — you could build one as a dict, if for some weird reason you couldn’t import collections, or, in pre-2.7 Python as a collections.defaultdict(int), using the items as keys and the associated value as the count).

Checking for membership of a value in a set (or dict, for keys) is blazingly fast (taking about a constant, short time), while in a list it takes time proportional to the list’s length in the average and worst cases. So, if you have hashable items, don’t care either way about order or duplicates, and want speedy membership checking, set is better than list.


回答 1

  • 您是否只需要订购的物品序列?取得清单。
  • 你只需要知道你是否已经一个特定的值,但不排序(你不需要存储复本)?使用一套。
  • 您是否需要将值与键相关联,以便稍后可以有效地(通过键)查找它们?使用字典。
  • Do you just need an ordered sequence of items? Go for a list.
  • Do you just need to know whether or not you’ve already got a particular value, but without ordering (and you don’t need to store duplicates)? Use a set.
  • Do you need to associate values with keys, so you can look them up efficiently (by key) later on? Use a dictionary.

回答 2

如果您想要无序的唯一元素集合,请使用set。(例如,当您要在文档中使用所有单词的集合时)。

当您想要收集元素的不可变的有序列表时,请使用tuple。(例如,当您希望将(名称,phone_number)对用作集合中的元素时,您将需要一个元组而不是一个列表,因为集合要求元素是不可变的。

当您想收集元素的可变的有序列表时,请使用list。(例如,当您要将新的电话号码追加到列表中时:[number1,number2,…])。

当您想要从键到值的映射时,请使用dict。(例如,当您需要将姓名映射到电话号码的电话簿时:){'John Smith' : '555-1212'}。请注意,字典中的键是无序的。(如果您遍历字典(电话簿),则按键(名称)可能以任何顺序显示)。

When you want an unordered collection of unique elements, use a set. (For example, when you want the set of all the words used in a document).

When you want to collect an immutable ordered list of elements, use a tuple. (For example, when you want a (name, phone_number) pair that you wish to use as an element in a set, you would need a tuple rather than a list since sets require elements be immutable).

When you want to collect a mutable ordered list of elements, use a list. (For example, when you want to append new phone numbers to a list: [number1, number2, …]).

When you want a mapping from keys to values, use a dict. (For example, when you want a telephone book which maps names to phone numbers: {'John Smith' : '555-1212'}). Note the keys in a dict are unordered. (If you iterate through a dict (telephone book), the keys (names) may show up in any order).


回答 3

  • 当您有一组映射到值的唯一键时,请使用字典。

  • 如果您有项目的有序集合,请使用列表。

  • 使用一组存储一组无序的项目。

  • Use a dictionary when you have a set of unique keys that map to values.

  • Use a list if you have an ordered collection of items.

  • Use a set to store an unordered set of items.


回答 4

简而言之,使用:

list -如果您需要订购的物品序列。

dict -如果您需要将值与键相关联

set -如果您需要保留唯一元素。

详细说明

清单

列表是可变序列,通常用于存储同类项目的集合。

列表实现了所有常见的序列操作:

  • x in lx not in l
  • l[i]l[i:j]l[i:j:k]
  • len(l)min(l)max(l)
  • l.count(x)
  • l.index(x[, i[, j]])-的第一出现的索引xl(在或之后i和之前j的indeces)

列表还实现了所有可变序列操作:

  • l[i] = x-项目il被替换x
  • l[i:j] = tlito的切片j被iterable的内容替换t
  • del l[i:j] – 如同 l[i:j] = []
  • l[i:j:k] = t-的元素l[i:j:k]已替换为t
  • del l[i:j:k]s[i:j:k]从列表中删除的元素
  • l.append(x)-追加x到序列的末尾
  • l.clear()-从中删除所有项目l(与del相同l[:]
  • l.copy()-创建的浅表副本l(与相同l[:]
  • l.extend(t)l += t-扩展l以下内容t
  • l *= n-更新l其内容重复n
  • l.insert(i, x)-插入xl由下式给出的指数在i
  • l.pop([i])-在处检索项目,i并将其从中删除l
  • l.remove(x)-从等于x的l位置删除第一项l[i]
  • l.reverse()-反转l到位的项目

利用方法append和可以将列表用作堆栈pop

字典

字典将可散列的值映射到任意对象。字典是可变对象。字典的主要操作是使用一些键存储值并提取给定键的值。

在字典中,不能将不可哈希的值(即包含列表,字典或其他可变类型的值)用作键。

集合是不同的可哈希对象的无序集合。集合通常用于进行成员资格测试,从序列中删除重复项以及计算数学运算(例如交集,并集,差和对称差)。

In short, use:

list – if you require an ordered sequence of items.

dict – if you require to relate values with keys

set – if you require to keep unique elements.

Detailed Explanation

List

A list is a mutable sequence, typically used to store collections of homogeneous items.

A list implements all of the common sequence operations:

  • x in l and x not in l
  • l[i], l[i:j], l[i:j:k]
  • len(l), min(l), max(l)
  • l.count(x)
  • l.index(x[, i[, j]]) – index of the 1st occurrence of x in l (at or after i and before j indeces)

A list also implements all of the mutable sequence operations:

  • l[i] = x – item i of l is replaced by x
  • l[i:j] = t – slice of l from i to j is replaced by the contents of the iterable t
  • del l[i:j] – same as l[i:j] = []
  • l[i:j:k] = t – the elements of l[i:j:k] are replaced by those of t
  • del l[i:j:k] – removes the elements of s[i:j:k] from the list
  • l.append(x) – appends x to the end of the sequence
  • l.clear() – removes all items from l (same as del l[:])
  • l.copy() – creates a shallow copy of l (same as l[:])
  • l.extend(t) or l += t – extends l with the contents of t
  • l *= n – updates l with its contents repeated n times
  • l.insert(i, x) – inserts x into l at the index given by i
  • l.pop([i]) – retrieves the item at i and also removes it from l
  • l.remove(x) – remove the first item from l where l[i] is equal to x
  • l.reverse() – reverses the items of l in place

A list could be used as stack by taking advantage of the methods append and pop.

Dictionary

A dictionary maps hashable values to arbitrary objects. A dictionary is a mutable object. The main operations on a dictionary are storing a value with some key and extracting the value given the key.

In a dictionary, you cannot use as keys values that are not hashable, that is, values containing lists, dictionaries or other mutable types.

Set

A set is an unordered collection of distinct hashable objects. A set is commonly used to include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.


回答 5

尽管这并不涵盖sets,但这是对dicts和lists 的很好解释:

列表看起来就是-值列表。它们中的每一个都从零开始编号-第一个从零开始编号,第二个为1,第三个为2,依此类推。您可以从列表中删除值,并在末尾添加新值。例如:您的许多猫的名字。

字典类似于其名称所暗示的内容-字典。在字典中,您有单词的“索引”,并且每个单词都有一个定义。在python中,单词称为“键”,而定义称为“值”。字典中的值未编号-类似于其名称所建议的名称-字典。在字典中,您有单词的“索引”,并且每个单词都有一个定义。字典中的值没有编号-它们也没有任何特定的顺序-键执行相同的操作。您可以添加,删除和修改字典中的值。例如:电话簿。

http://www.sthurlow.com/python/lesson06/

Although this doesn’t cover sets, it is a good explanation of dicts and lists:

Lists are what they seem – a list of values. Each one of them is numbered, starting from zero – the first one is numbered zero, the second 1, the third 2, etc. You can remove values from the list, and add new values to the end. Example: Your many cats’ names.

Dictionaries are similar to what their name suggests – a dictionary. In a dictionary, you have an ‘index’ of words, and for each of them a definition. In python, the word is called a ‘key’, and the definition a ‘value’. The values in a dictionary aren’t numbered – tare similar to what their name suggests – a dictionary. In a dictionary, you have an ‘index’ of words, and for each of them a definition. The values in a dictionary aren’t numbered – they aren’t in any specific order, either – the key does the same thing. You can add, remove, and modify the values in dictionaries. Example: telephone book.

http://www.sthurlow.com/python/lesson06/


回答 6

对于C ++,我始终牢记以下流程图:在哪种情况下,我使用特定的STL容器?,所以我很好奇Python3是否也有类似的东西,但是我没有运气。

对于Python,需要记住的是:没有像C ++一样的Python标准。因此,不同的Python解释器(例如CPython,PyPy)可能会有巨大的差异。以下流程图适用于CPython。

另外,我发现包含以下数据结构到图中,没有什么好办法:bytesbyte arraystuplesnamed_tuplesChainMapCounter,和arrays

  • OrderedDict并且deque可以通过collections模块获得。
  • heapq可从heapq模块中获得
  • LifoQueueQueuePriorityQueue可以通过queue专门用于并发(线程)访问的模块获得。(也有一个multiprocessing.Queue可用的,但我不知道与它之间的区别,queue.Queue但是假设需要从进程进行并发访问时应该使用它。)
  • dictsetfrozen_set,和list被内置当然

对于任何人,如果您可以改善此答案并在各个方面提供更好的图表,我将不胜感激。随时欢迎。

PS:该图已通过yed制作。graphml文件在这里

For C++ I was always having this flow chart in mind: In which scenario do I use a particular STL container?, so I was curious if something similar is available for Python3 as well, but I had no luck.

What you need to keep in mind for Python is: There is no single Python standard as for C++. Hence there might be huge differences for different Python interpreters (e.g. CPython, PyPy). The following flow chart is for CPython.

Additionally I found no good way to incorporate the following data structures into the diagram: bytes, byte arrays, tuples, named_tuples, ChainMap, Counter, and arrays.

  • OrderedDict and deque are available via collections module.
  • heapq is available from the heapq module
  • LifoQueue, Queue, and PriorityQueue are available via the queue module which is designed for concurrent (threads) access. (There is also a multiprocessing.Queue available but I don’t know the differences to queue.Queue but would assume that it should be used when concurrent access from processes is needed.)
  • dict, set, frozen_set, and list are builtin of course

For anyone I would be grateful if you could improve this answer and provide a better diagram in every aspect. Feel free and welcome.

PS: the diagram has been made with yed. The graphml file is here


回答 7

结合列表字典集合,还有另一个有趣的python对象OrderedDicts

顺序词典与常规词典一样,但是它们记住项目插入的顺序。在有序字典上进行迭代时,将按照项的键首次添加的顺序返回项。

当您需要保留键的顺序(例如处理文档)时,OrderedDicts可能会很有用:通常需要文档中所有术语的向量表示。因此,使用OrderedDicts,您可以有效地验证术语是否已被阅读过,添加术语,提取术语,以及在所有操作之后可以提取它们的有序矢量表示。

In combination with lists, dicts and sets, there are also another interesting python objects, OrderedDicts.

Ordered dictionaries are just like regular dictionaries but they remember the order that items were inserted. When iterating over an ordered dictionary, the items are returned in the order their keys were first added.

OrderedDicts could be useful when you need to preserve the order of the keys, for example working with documents: It’s common to need the vector representation of all terms in a document. So using OrderedDicts you can efficiently verify if a term has been read before, add terms, extract terms, and after all the manipulations you can extract the ordered vector representation of them.


回答 8

列表就是它们的外观-值列表。它们中的每一个都从零开始编号-第一个从零开始编号,第二个为1,第三个为2,依此类推。您可以从列表中删除值,并在末尾添加新值。例如:您的许多猫的名字。

元组就像列表一样,但是您不能更改它们的值。首先给出的值是程序其余部分所保持的值。同样,每个值都从零开始编号,以方便参考。示例:一年中的月份名称。

字典类似于其名称所暗示的内容-字典。在字典中,您有单词的“索引”,并且每个单词都有一个定义。在python中,单词称为“键”,而定义称为“值”。字典中的值未编号-类似于其名称所建议的名称-字典。在字典中,您有单词的“索引”,并且每个单词都有一个定义。在python中,单词称为“键”,而定义称为“值”。字典中的值没有编号-它们也没有任何特定的顺序-键执行相同的操作。您可以添加,删除和修改字典中的值。例如:电话簿。

Lists are what they seem – a list of values. Each one of them is numbered, starting from zero – the first one is numbered zero, the second 1, the third 2, etc. You can remove values from the list, and add new values to the end. Example: Your many cats’ names.

Tuples are just like lists, but you can’t change their values. The values that you give it first up, are the values that you are stuck with for the rest of the program. Again, each value is numbered starting from zero, for easy reference. Example: the names of the months of the year.

Dictionaries are similar to what their name suggests – a dictionary. In a dictionary, you have an ‘index’ of words, and for each of them a definition. In python, the word is called a ‘key’, and the definition a ‘value’. The values in a dictionary aren’t numbered – tare similar to what their name suggests – a dictionary. In a dictionary, you have an ‘index’ of words, and for each of them a definition. In python, the word is called a ‘key’, and the definition a ‘value’. The values in a dictionary aren’t numbered – they aren’t in any specific order, either – the key does the same thing. You can add, remove, and modify the values in dictionaries. Example: telephone book.


回答 9

在使用它们时,我会详尽列出它们的方法,以供您参考:

class ContainerMethods:
    def __init__(self):
        self.list_methods_11 = {
                    'Add':{'append','extend','insert'},
                    'Subtract':{'pop','remove'},
                    'Sort':{'reverse', 'sort'},
                    'Search':{'count', 'index'},
                    'Entire':{'clear','copy'},
                            }
        self.tuple_methods_2 = {'Search':'count','index'}

        self.dict_methods_11 = {
                    'Views':{'keys', 'values', 'items'},
                    'Add':{'update'},
                    'Subtract':{'pop', 'popitem',},
                    'Extract':{'get','setdefault',},
                    'Entire':{ 'clear', 'copy','fromkeys'},
                            }
        self.set_methods_17 ={
                    'Add':{['add', 'update'],['difference_update','symmetric_difference_update','intersection_update']},
                    'Subtract':{'pop', 'remove','discard'},
                    'Relation':{'isdisjoint', 'issubset', 'issuperset'},
                    'operation':{'union' 'intersection','difference', 'symmetric_difference'}
                    'Entire':{'clear', 'copy'}}

When use them, I make an exhaustive cheatsheet of their methods for your reference:

class ContainerMethods:
    def __init__(self):
        self.list_methods_11 = {
                    'Add':{'append','extend','insert'},
                    'Subtract':{'pop','remove'},
                    'Sort':{'reverse', 'sort'},
                    'Search':{'count', 'index'},
                    'Entire':{'clear','copy'},
                            }
        self.tuple_methods_2 = {'Search':'count','index'}

        self.dict_methods_11 = {
                    'Views':{'keys', 'values', 'items'},
                    'Add':{'update'},
                    'Subtract':{'pop', 'popitem',},
                    'Extract':{'get','setdefault',},
                    'Entire':{ 'clear', 'copy','fromkeys'},
                            }
        self.set_methods_17 ={
                    'Add':{['add', 'update'],['difference_update','symmetric_difference_update','intersection_update']},
                    'Subtract':{'pop', 'remove','discard'},
                    'Relation':{'isdisjoint', 'issubset', 'issuperset'},
                    'operation':{'union' 'intersection','difference', 'symmetric_difference'}
                    'Entire':{'clear', 'copy'}}

回答 10

字典:Python字典的用法类似于哈希表,其键为索引,对象为值。

列表:列表用于将对象保存在数组中,该对象由该对象在数组中的位置索引。

集合:集合是具有函数的集合,这些函数可以判断集合中是否存在对象。

Dictionary: A python dictionary is used like a hash table with key as index and object as value.

List: A list is used for holding objects in an array indexed by position of that object in the array.

Set: A set is a collection with functions that can tell if an object is present or not present in the set.