简体   繁体   中英

Python 2D array assignment

In Matlab I can assign values inside arrays as follows.

a = [];
a(end+1, 1:2) = [1,2];
a(end,3:4) = [3,4];
a(end+1, 1:2) = [5,6];
a(end,3:4) = [7,8];

and so on. But in Python I can use the append command to append an array to the existing array. eg

a = []
a.append([1,2,3,4])
a.append([5,6,7,8])

My problem is I should assign the first two values at some point and the next two values in some other point as shown in my Matlab code. How can I do that?

I think you're looking for extend :

a = []
a.append([1,2])  # [[1,2]]
a[-1].extend([3,4])  # [[1,2,3,4]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM