简体   繁体   English

在元组内重复一个元组

[英]Repeat a tuple inside a tuple

Is there a way to repeat a tuple inside a tuple ? 有没有办法在元组中重复一个元组?

If I do something like 如果我做的事情

a = ((0, 1) * n)

I still get 我还是得到的

a = (0, 1, 0, 1 ..... n times) 

what if I want something like 如果我想要类似的东西怎么办?

a = ((0, 1), (0, 1) ... n times)

Multiply a tuple with a tuple as its item. 将元组与元组相乘作为项目。 Don't forget a trailing , . 不要忘记尾随,

>>> ((0, 1),) * 5
((0, 1), (0, 1), (0, 1), (0, 1), (0, 1))

You might also be interested in a generator. 您可能也对发电机感兴趣。

>>> def f():
...     for i in range(10):
...         yield (0, 1)
... 
>>> tuple(f())
((0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1))

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

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