简体   繁体   English

python元组乘以某个常数意味着什么?

[英]What does the python tuple multiplied by some constant mean?

I read some python codes like follows: 我读了一些python代码,如下所示:

color = (1.0,)*4

I couldn't figure out what does it mean? 我不知道这是什么意思? (1.0,) means some tuple, but what does it mean by multiplying 4 here? (1.0,)表示一些元组,但是在这里乘以4是什么意思?

You create a new tuple with 4 times the same referenced value. 您创建一个新的元组,其引用值是相同参考值的4倍。

>>> (1.0,) * 4
(1.0, 1.0, 1.0, 1.0)

See the Sequence types reference 请参阅序列类型参考

s * n, n * s
n shallow copies of s concatenated n s浅表副本

Note that it's the exact same value that is reused; 注意,重用的是完全相同的值 you see this when you use a mutable value: 当您使用可变值时,您会看到以下内容:

>>> lst = []
>>> tup = (lst,) * 4
>>> tup[0] is lst
True
>>> all(i is lst for i in tup)
True

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

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