简体   繁体   English

字典与键/值对

[英]Dictionaries vs key/value pairs

I understand that a dictionary refers to a collection of key/value pairs, but I'm confused on a few of the finer details.我知道字典是指键/值对的集合,但我对一些更精细的细节感到困惑。

Does a dictionary need to contain more than one key value pair?字典是否需要包含多个键值对? Example 1:示例 1:

person = {'first_name': 'John’} 

Is this a dictionary even though it only has 1 key & 1 value?这是一本字典,即使它只有 1 个键和 1 个值? Or would it need at least a 2nd key/value pair to really quality还是至少需要第二个键/值对才能真正提高质量

Also, dictionaries seem very common in an API context.此外,字典在 API 上下文中似乎很常见。 Here are some other examples/question I have.这是我的其他一些示例/问题。 Example 2:示例 2:

'itemFilter': [
    {'name': 'Condition', 'value': 'New'}
]       

Is this a dictionary?这是字典吗? A list of dictionaries?字典列表? Or is it a list of key value pairs.或者它是一个键值对列表。 Wouldn't a list of dictionaries be something like:字典列表不会是这样的:

list_of_dicts = [{‘key1’:’value1’},{‘key2’:’value2’}]

Also, does it need to have the '=' to be considered a dict / list of dicts?另外,是否需要将“=”视为字典/字典列表? Or does the colon suffice here还是冒号在这里就足够了

Example 3:示例 3:

 'paginationInput': {
        'entriesPerPage': 10,
        'pageNumber': 1
    }

Is this 2 dictionaries?这是2个字典吗? Or a single dictionary with 2 key/value pairs.或具有 2 个键/值对的单个字典。 Im inclined to think it is the latter, since there is only one set of brackets denoted therein我倾向于认为是后者,因为其中只有一组括号

Technically you can have an empty dictionary :从技术上讲,您可以有一个空字典

empty_dictionary = {}
empty_dictionary = dict()

So, no, a dictionary doesn't need to include multiple key value pairs.所以,不,字典不需要包含多个键值对。

Your second example is a list that has a single item which is a dictionary.您的第二个示例是一个列表,其中包含一个字典项目。 A list also can have a single item, I hope that answers your next question.一个列表也可以有一个项目,我希望能回答你的下一个问题。

Yes, you need to use the assignment operator of = to define your dictionary with that syntax.是的,您需要使用 = 的赋值运算符来使用该语法定义您的字典。

paginationInput = {
        'entriesPerPage': 10,
        'pageNumber': 1
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM