2
)我想創建一個基於另一個列表的列表,連續重複3次相同的值。在python列表理解中解壓元組(
目前,我使用:
>>> my_list = [ 1, 2 ]
>>> three_times = []
>>> for i in range(len(my_list)):
... for j in range(3):
... three_times.append(my_list[ i ])
...
>>> print three_times
[1, 1, 1, 2, 2, 2]
但我想這樣做使用更Python的方式,如:
>>> my_list = [ 1, 2 ]
>>> three_times = []
>>> three_times = [ (value,) * 3 for value in my_list ]
>>> print three_times
[(1, 1, 1), (2, 2, 2)]
不過,我不能找到一種方法,解壓元組。
像three_times = [ *((value,) * 3) for value in my_list ]
這樣的東西可以完美解開元組,但這不是一個正確的語法。