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

是否存在更适合每种数据类型的场景?


当前回答

虽然这里不包括集合,但它很好地解释了字典和列表:

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/

其他回答

虽然这里不包括集合,但它很好地解释了字典和列表:

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/

简而言之,使用:

列表-如果你需要一个有序的项目序列。

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是不同哈希对象的无序集合。集合通常用于包括成员测试,从序列中删除重复项,以及计算数学操作,如交集、并集、差分和对称差分。

列表就是它们看起来的那样——一个值的列表。每一个都有编号,从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.

当您有一组映射到值的唯一键时,请使用字典。 如果你有一个有序的项目集合,可以使用列表。 使用集合存储一组无序的项。

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

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

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

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