There are many parallels between lists and dictionaries. Like lists, dictionaries allow several, even many, values to be stored in one variable. Each element in a list has a unique integer index, and these integer indices must increase sequentially from zero. Similarly, each value in a dictionary has a unique key, but a dictionary’s keys are more flexible than a list’s indices. A dictionary’s keys can be integers. They can also be floating-point numbers or strings. When the keys are numeric, they do not have to start from zero, nor do they have to be sequential. When the keys are strings, they can be any combination of characters, including the empty string. All of the keys in a dictionary must be distinct, just as all of the indices in a list are distinct.

错误:搜索内容不能为空,请输入英文关键词
错误:关键词超出字数限制,请精简
高级检索

Dictionaries

  • Ben Stephenson

摘要

There are many parallels between lists and dictionaries. Like lists, dictionaries allow several, even many, values to be stored in one variable. Each element in a list has a unique integer index, and these integer indices must increase sequentially from zero. Similarly, each value in a dictionary has a unique key, but a dictionary’s keys are more flexible than a list’s indices. A dictionary’s keys can be integers. They can also be floating-point numbers or strings. When the keys are numeric, they do not have to start from zero, nor do they have to be sequential. When the keys are strings, they can be any combination of characters, including the empty string. All of the keys in a dictionary must be distinct, just as all of the indices in a list are distinct.