什么时候应该使用字典、列表或集合?
是否存在更适合每种数据类型的场景?
什么时候应该使用字典、列表或集合?
是否存在更适合每种数据类型的场景?
当前回答
当您有一组映射到值的唯一键时,请使用字典。 如果你有一个有序的项目集合,可以使用列表。 使用集合存储一组无序的项。
其他回答
在使用它们的时候,我为它们的方法做了一个详尽的备忘单,供你参考:
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'}}
列表就是它们看起来的那样——一个值的列表。每一个都有编号,从0开始——第一个编号为0,第二个编号为1,第三个编号为2,以此类推。您可以从列表中删除值,并在末尾添加新值。例子:你的许多猫的名字。
元组就像列表一样,但是你不能改变它们的值。你首先给出的值,就是你在接下来的程序中一直用到的值。同样,每个值都从0开始编号,以便于参考。例如:一年中月份的名称。
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.
虽然这里不包括集合,但它很好地解释了字典和列表:
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/
list保持顺序,dict和set则不然:因此,当您关心顺序时,必须使用list(当然,如果您选择的容器仅限于这三种;-))。
Dict将每个键与值关联,而list和set只包含值:显然,这是非常不同的用例。
Set要求项是可哈希的,list不需要:因此,如果你有不可哈希的项,你不能使用Set,而必须使用list。
集合禁止重复,列表不重复:也是一个至关重要的区别。(在集合中可以找到一个“multiset”,它将重复项映射到一个不同的计数中,用于出现一次以上的项目。Counter——如果出于某种奇怪的原因无法导入集合,可以将其构建为dict,或者在2.7之前的Python中,将其作为collections.defaultdict(int),使用项作为键,并将相关值作为计数)。
在一个集合(或dict,键)中检查一个值的成员关系非常快(大约需要一个常数,很短的时间),而在一个列表中,在平均和最坏的情况下,它所花费的时间与列表的长度成正比。所以,如果你有可哈希的项,不关心顺序或重复,并且想要快速的成员检查,set比list更好。
简而言之,使用:
列表-如果你需要一个有序的项目序列。
Dict -如果你需要将值与键联系起来
设置—如果您需要保留唯一的元素。
详细解释
List
列表是一个可变序列,通常用于存储同构项的集合。
列表实现了所有常见的序列操作:
X在l里和X不在l里 L [i], L [i:j], L [i:j:k] Len (l) min(l) max(l) l.count (x) l.index(x[, i[, j]]) - x在l中第一次出现的索引(在i前后和j索引之前)
list还实现了所有可变序列操作:
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
可以利用append和pop方法将列表用作堆栈。
字典
字典将可哈希值映射到任意对象。字典是一个可变对象。字典的主要操作是存储带有某个键的值,并在给定键的情况下提取值。
在字典中,不能使用不可哈希的值作为键,即包含列表、字典或其他可变类型的值。
Set
set是不同哈希对象的无序集合。集合通常用于包括成员测试,从序列中删除重复项,以及计算数学操作,如交集、并集、差分和对称差分。