2016-08-01 50 views

回答

2

使用list comprehension

list_of_nums = [5, 6, 7, 8] 
result = [(15, num) for num in list_of_nums] 

只要你有生產基於另一個序列的輸出列表的過程中,通常一個列表解析可以做的工作。

演示:

>>> list_of_nums = [5, 6, 7, 8] 
>>> [(15, num) for num in list_of_nums] 
[(15, 5), (15, 6), (15, 7), (15, 8)] 
0

您也可以嘗試zip功能

result = zip([15]*len(list_of_nums),list_of_nums) 

print(result) 
[(15, 5), (15, 6), (15, 7), (15, 8)]