1
假设我有一个数字的数15
和列表:元组一个号码与数字蟒蛇的列表
[5, 6, 7, 8]
现在我想打一个元组列表像
[(15,6), (15,5), (15,7), (15,8)]
一个人如何做那很快?
假设我有一个数字的数15
和列表:元组一个号码与数字蟒蛇的列表
[5, 6, 7, 8]
现在我想打一个元组列表像
[(15,6), (15,5), (15,7), (15,8)]
一个人如何做那很快?
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)]
您也可以尝试zip
功能
result = zip([15]*len(list_of_nums),list_of_nums)
print(result)
[(15, 5), (15, 6), (15, 7), (15, 8)]