python - 使用python迭代直到第二个数组的末尾

标签 python list python-3.x tuples

我做过这样的事情:

d = [('e', 0), ('f', 1), ('e', 0), ('f', 1)]
e = ['a']
d = [(n,j) for n,(i,j) in zip(e,d)]
d
[('a',0)]

我只是想用数组值替换等效的元组值,而不更改关联的数字。但是该列表只到数组 e 的 len,而不是 d。我想得到的输出是这样的:

d
[('a', 0), ('f', 1), ('e', 0), ('f', 1)]

最佳答案

只需将d未处理的尾部添加到已处理的部分即可:

[(n,j) for n,(i,j) in zip(e,d)] + d[len(e):]
#[('a', 0), ('f', 1), ('e', 0), ('f', 1)]

关于python - 使用python迭代直到第二个数组的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45453949/

相关文章:

python - 如何在 numpy/scipy 中实现像素数学

entity-framework - group by 之后返回 List<string>

c# - 从列表中获取值

python - 使用 pip 在 Windows 10 上安装 Jupyter 时出错

python - 如何使用 Python 和 GTK3+ 将 PDF 渲染为 Pixbuf

python - 自定义对象训练 Tensor-flow 错误

python - 从列 DataFrame 中的不同值创建索引/行

python - 如何根据 python pandas 中与前一行的差异来移动列值?

r - 代码重构 : from $ to [[

python - 尝试为 paramiko SSHClient 指定 key ,错误 : Could not deserialize key data