Python – 将自定义列添加到元组列表
有时,在使用Python记录时,我们可能会遇到需要将自定义列添加到元组列表的问题。这类问题可以应用于数据领域,例如 Web 开发。让我们讨论可以执行此任务的某些方式。
Input :
test_list = [(3, ), (7, ), (2, )]
cus_eles = [7, 8, 2]
Output : [(3, 7), (7, 8), (2, 2)]
Input :
test_list = [(3, 9, 6, 10)]
cus_eles = [7]
Output : [(3, 9, 6, 10, 7)]
方法 #1:使用列表理解 + zip()
上述功能的组合可以用来解决这个问题。在此,我们在 zip() 的帮助下执行自定义元素和元组的配对。
# Python3 code to demonstrate working of
# Add Custom Column to Tuple list
# Using list comprehension + zip()
# initializing list
test_list = [(3, 4), (78, 76), (2, 3)]
# printing original list
print("The original list is : " + str(test_list))
# initializing add list
cus_eles = [17, 23, 12]
# Add Custom Column to Tuple list
# Using list comprehension + zip()
res = [sub + (val, ) for sub, val in zip(test_list, cus_eles)]
# printing result
print("The tuples after adding elements : " + str(res))
输出 :
The original list is : [(3, 4), (78, 76), (2, 3)]
The tuples after adding elements : [(3, 4, 17), (78, 76, 23), (2, 3, 12)]
方法 #2:使用map()
+ lambda
上述功能的组合也可以用来解决这个问题。在此,我们使用 map() 执行将逻辑扩展到每个元组的任务,而 lambda 用于执行加法任务。
# Python3 code to demonstrate working of
# Add Custom Column to Tuple list
# Using map() + lambda
# initializing list
test_list = [(3, 4), (78, 76), (2, 3)]
# printing original list
print("The original list is : " + str(test_list))
# initializing add list
cus_eles = [17, 23, 12]
# Add Custom Column to Tuple list
# Using map() + lambda
res = list(map(lambda a, b: a +(b, ), test_list, cur_eles))
# printing result
print("The tuples after adding elements : " + str(res))
输出 :
The original list is : [(3, 4), (78, 76), (2, 3)]
The tuples after adding elements : [(3, 4, 17), (78, 76, 23), (2, 3, 12)]