python - 将元素列表组合成元组列表

标签 python

如何将元素列表与元组列表结合起来(如下所示)?

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

预期输出:

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

最佳答案

使用extended iterable unpacking构建预期结果的元组:

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

输出

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

作为替代方案,使用:

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

关于python - 将元素列表组合成元组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73096475/

相关文章:

python - nginx/gunicorn Django Rest Framework 应用的端口代理

python - 在 python 中交换字典列表中的键

Python:没有得到列表的最长公共(public)子序列的长度

python - Pandas 按自定义功能分组

python - setuptools_scm : how to append "-alpha" to package version upon merge in gitflow?

python - List 在 Python 中真的是不变的吗

python - 将表达式解析为列表

Python + pexpect - 如何建立 ssh 连接?

python - 登录到远程机器并运行脚本

python - Flask 是否有任何音频流解决方案?