Python 中 N 元素增量元組
當需要建立“N”個元素遞增元組時,可以使用生成器表示式和“元組”方法。
以下是同一展示 -
示例
N = 3
print("The value of 'N' has been initialized")
print("The number of times it has to be repeated is : ")
print(N)
my_result = tuple((elem, ) * N for elem in range(1, 6))
print("The tuple sequence is : ")
print(my_result)輸出
The value of 'N' has been initialized The number of times it has to be repeated is : 3 The tuple sequence is : ((1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5))
解釋
- 初始化“N”的值,並在控制檯上顯示。
- 將具有特定元素的元組,在給定範圍內乘以“N”次,分配給變數。
- 生成此序列,並在控制檯上顯示為輸出。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP