什么时候应该使用字典、列表或集合?
是否存在更适合每种数据类型的场景?
什么时候应该使用字典、列表或集合?
是否存在更适合每种数据类型的场景?
当前回答
虽然这里不包括集合,但它很好地解释了字典和列表:
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/
字典:当你想用索引以外的东西来查找某个东西时。例子:
dictionary_of_transport = {
"cars": 8,
"boats": 2,
"planes": 0
}
print("I have the following amount of planes:")
print(dictionary_of_transport["planes"])
#Output: 0
列表和集:当您想要添加和删除值时。 列表:使用索引查找值 集:存储值,但不能使用任何方法访问它们。
列表就是它们看起来的那样——一个值的列表。每一个都有编号,从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.
在使用它们的时候,我为它们的方法做了一个详尽的备忘单,供你参考:
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'}}