list 如何在python列表中附加行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42479988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:12:41  来源:igfitidea点击:

How to append rows in a python list?

python-2.7listappend

提问by dSb

I want to append a row in a python list.

我想在 python 列表中附加一行。

Below is what I am trying,

下面是我正在尝试的,

# Create an empty array
arr=[]
values1 = [32, 748, 125, 458, 987, 361]
arr = np.append(arr, values1)
print arr

[ 32. 748. 125. 458. 987. 361.]

[ 32. 748. 125. 458. 987. 361. ]

I want to append second row in the list, so that I will get an array like

我想在列表中附加第二行,这样我就会得到一个数组

[ [32. 748. 125. 458. 987. 361.], [42. 344. 145. 448. 187. 304.]]

[ [32. 748. 125. 458. 987. 361.], [42. 344. 145. 448. 187. 304.]]

I am getting error when I try to add second row

当我尝试添加第二行时出现错误

values2 = [42, 344, 145, 448, 187, 304]    
arr = np.append(arr, values2)

How to do that?

怎么做?

回答by Hugo

Just append directly to your original list:

只需直接附加到您的原始列表中:

# Create an empty list
my_list = []
values1 = [32, 748, 125, 458, 987, 361]
my_list.append(values1)
print(my_list)

values2 = [42, 344, 145, 448, 187, 304]    
my_list.append(values2)
print(my_list)

And this will be your output:

这将是您的输出:

[[32, 748, 125, 458, 987, 361]]
[[32, 748, 125, 458, 987, 361], [42, 344, 145, 448, 187, 304]]

Hope that helps!

希望有帮助!