gpt4 book ai didi

python - 优雅地扩展几个列表

转载 作者:行者123 更新时间:2023-11-28 20:28:38 26 4
gpt4 key购买 nike

给定几个列表:

a = ["a1", "a2", "a3"]
b = ["b1", "b2", "b3"]
...
n = ["n1", "n2", "n3"]

以及新值列表:

new_vals = ["a4", "b4", "n4"]

我想得到:

["a1", "a2", "a3", "a4"]
["b1", "b2", "b3", "b4"]
...
["n1", "n2", "n3", "n4"]

当然,我可以用循环和临时变量来做到这一点。似乎 zipmaplist.extend 的组合应该做得更优雅,但我无法理解。

最佳答案

像这样:

a = ["a1", "a2", "a3"]
b = ["b1", "b2", "b3"]

# Put the list a, b ... in a big_list.
big_list = [a, b]

new_vals = ["a4", "b4", "n4"]

for i, new_val in enumerate(new_vals):
big_list[i].append(new_val)

关于python - 优雅地扩展几个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4641209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com