Python 字典的值和键都可以是整数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44814719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:29:37  来源:igfitidea点击:

Can both the values and keys of a dictionary be integers?

python

提问by mathexplorer

Can both the values and keys of a dictionary be integers in python? Or do I need one of them to be like a string or something?

python中字典的值和键都可以是整数吗?或者我需要其中一个像字符串一样吗?

回答by alkasm

Sure! From the python docs:

当然!从 python 文档:

5.5. Dictionaries

Another useful data type built into Python is the dictionary (see Mapping Types — dict). Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys. Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. You can't use lists as keys, since lists can be modified in place using index assignments, slice assignments, or methods like append() and extend().

5.5. 字典

Python 内置的另一种有用的数据类型是字典(请参阅映射类型 — dict)。字典有时在其他语言中被称为“关联记忆”或“关联数组”。与由一系列数字索引的序列不同,字典由键索引,键可以是任何不可变类型;字符串和数字始终可以是键。如果元组仅包含字符串、数字或元组,则它们可以用作键;如果元组直接或间接包含任何可变对象,则它不能用作键。您不能使用列表作为键,因为可以使用索引分配、切片分配或 append() 和 extend() 等方法就地修改列表。

You can also try it out super quickly:

你也可以超级快速地尝试一下:

>>> dict = {1:0, 2:1}
>>> dict[1]
0
>>> dict[2]
1

I like one of the examples on the page as it uses a dictionary comprehension (new in 2.7+) in a way that works like a function:

我喜欢页面上的一个示例,因为它以一种类似于函数的方式使用字典理解(2.7+ 中的新功能):

>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}

Since it works for any immutable typeyou can even use floats for keys:

由于它适用于任何不可变类型,您甚至可以使用浮点数作为键:

>>> {x: x**2 for x in (1, 1.5, 2)}
{1: 1, 1.5: 2.25, 2: 4}

And again, another common immutable type in python are tuples, (..., ..., ...)which you can also use for keys:

同样,python 中另一种常见的不可变类型是元组,(..., ..., ...)您也可以将其用于键:

>>> {(x,y): (x**2,y**2) for x in range(3) for y in range(2)}
{(0, 0): (0, 0), 
(0, 1): (0, 1), 
(1, 0): (1, 0), 
(1, 1): (1, 1), 
(2, 0): (4, 0), 
(2, 1): (4, 1)}

回答by buxizhizhoum

Of course. Just take a very simple example: in python interpreter, input:

当然。举一个非常简单的例子:在python解释器中,输入:

a = {1:2}  # define an dict
a[1] # get the value whose key is 1

then you will get out put 2.

然后你会出去放2。

回答by veganaiZe

The key is always a string. It can be a number, but it is always a string.

键始终是一个字符串。它可以是一个数字,但它始终是一个字符串。

Yes.

是的。

https://docs.python.org/3/tutorial/datastructures.html#dictionaries

https://docs.python.org/3/tutorial/datastructures.html#dictionaries

"...can be any immutable type; strings and numbers can always be keys..."

“...可以是任何不可变类型;字符串和数字始终可以是键...”