我试图基于另一个列表创建一个列表,相同的值连续重复3次.
目前,我正在使用:
>>> 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,2,2]
但我想用更Pythonic的方式来做,例如:
>>> my_list = [ 1,2 ] >>> three_times = [] >>> three_times = [ (value,) * 3 for value in my_list ] >>> print three_times [(1,1),(2,2)]
对于my_list中的值,类似three_times = [*((value,)* 3)的东西对于解包元组是完美的,但这不是正确的语法.