在MATLAB命令窗口使用py.list()可以创建Python列表,然后可以使用Python中处理列表的函数操作它们。
下面在MATLAB命令窗口创建一个Python列表。
code.matlab
>> p=py.list([9,5,8,2,1])
p =
包含以下值的 Python list:
[9.0, 5.0, 8.0, 2.0, 1.0]
对Python列表中的元素从小到大进行排序。
code.matlab
>> p.sort()
>> p
p =
包含以下值的 Python list:
[1.0, 2.0, 5.0, 8.0, 9.0]
在Python列表后面添加两个元素。
code.matlab
>> p.extend([7,4])
>> p
p =
包含以下值的 Python list:
[1.0, 2.0, 5.0, 8.0, 9.0, 7.0, 4.0]
下面将一个元胞数组转换为Python列表。
code.matlab
>> students=py.list({'Lining','Mabo','Jihai'})
students=
Python list(不带属性)。
['Lining','Mabo','Jihai']
显示列表中学生的个数。
code.matlab
>> n=py.len(students)
n=
Python int with properties:
denominator: [1×1 py.int]
imag: [1×1 py.int]
numerator: [1×1 py.int]
real: [1×1 py.int]
3
下面创建Python列表,列表中的每个元素都是整数:
code.matlab
>> P=py.list({int32(1), int32(2), int32(3), int32(4)})
P=
Python list(不带属性)。
[1, 2, 3, 4]
显示值的数值类型。
code.matlab
>> class(P{1})
ans=
1×6 char array
py.int