简体   繁体   English

将元素列表组合到元组列表

[英]Combining Lists of elements to List of Tuples

How can I combine a List of elements with a List of Tuples (as shown below)?如何将元素列表与元组列表结合起来(如下所示)?

 a = ['x', 'y', 1234] b = [('Broad Street', 'NY'), ('Park Street', 'CA')]

Expected output:预期 output:

 [('x', 'y', 1234, 'Broad Street', 'NY'), ('x', 'y', 1234, 'Park Street', 'CA')]

Use extended iterable unpacking to build the tuples of the expected result:使用扩展的可迭代解包来构建预期结果的元组:

 res = [(*a, *bi) for bi in b] print(res)

Output Output

 [('x', 'y', 1234, 'Broad Street', 'NY'), ('x', 'y', 1234, 'Park Street', 'CA')]

As an alternative, use:作为替代方案,请使用:

 tuple_a = tuple(a) res = [tuple_a + bi for bi in b]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM