如何将这些元素附加到python中的数组?

Kri*_*ine 4 python list

input_elements = ["a", "b", "c", "d"]
my_array = ["1", "2", "3", "4"]
Run Code Online (Sandbox Code Playgroud)

我想要的输出是:

["1", "2", "3", "4", "a"]
["1", "2", "3", "4", "b"]
["1", "2", "3", "4", "c"]
["1", "2", "3", "4", "d"]
Run Code Online (Sandbox Code Playgroud)

我试过了:

for e in input_elements:
  my_array.append(e)
Run Code Online (Sandbox Code Playgroud)

我知道上面的代码是错误的,所以我想知道如何生成这样的输出.

Bha*_*Rao 5

您可以使用列表解析来解决您的问题.

>>> input_elements = ["a", "b", "c", "d"]
>>> my_array = ["1", "2", "3", "4"]
>>> [my_array+[i] for i in input_elements]
Run Code Online (Sandbox Code Playgroud)

结果看起来像

>>> from pprint import pprint
>>> pprint([my_array+[i] for i in input_elements])
[['1', '2', '3', '4', 'a'],
 ['1', '2', '3', '4', 'b'],
 ['1', '2', '3', '4', 'c'],
 ['1', '2', '3', '4', 'd']]
Run Code Online (Sandbox Code Playgroud)

请参阅"列表理解"是什么意思?它是如何工作的,我该如何使用它?有关它们的更多详细信息.