概述
由于本人对Python和Matlab都不是十分熟,而由于工作两个工具都要用,经常困惑于两者的相似性。现总结如下:
<一> Python的Dictionary和Matlab的struct的简单比较
Matlab的struct叫做Dictionary也许更合适一些.
Python中定义Dictionary:
>>> d = {"server":"mpilgrim", "database":"master"}
>> d = struct('server','mpilgrim', 'database','master')
>>> d["server"]
'mpilgrim'
>>> d["database"]
'master'
>> d.('server')
ans =
mpilgrim
>> d.('database')
ans =
master
在Python中不能通过value来获取key:
>>> d["mpilgrim"]
Traceback (innermost last):
KeyError: mpilgrim
>> d.('mpilgrim')
??? Reference to non-existent field 'mpilgrim'.
在Python中修改已经存在的key的value:
>>> d["database"] = "pubs"
>>> d
{'server': 'mpilgrim', 'database': 'pubs'}
>> d.('database')='pubs'
d =
>>> d["uid"] = "sa"
>>> d
{'server': 'mpilgrim', 'uid': 'sa', 'database': 'pubs'}
>> d.('uid')='sa'
d =
在Python中删除Dictionary的元素:
>>> d
{'database': 'master', 'uid': 'sa', 'server': 'mpilgrim'}
>>> del d['uid']
>>> d
{'database': 'master', 'server': 'mpilgrim'}
>> d=struct('database', 'master', 'uid', 'sa', 'server', 'mpilgrim')
d =
>> d=rmfield(d,'uid')
d =
>>> d.clear()
>>> d
{}
>> d=struct()
d =
1x1 struct array with no fields.
>>> d[42] = "douglas"
>>> d
{'server': 'mpilgrim', 'uid': 'sa', 'database': 'master',42: 'douglas', 'retrycount': 3}
>> d.123='123'
??? d.123='123'
Error: Missing MATLAB operator.
>> d.(123)='123'
??? Argument to dynamic structure reference must evaluate to a valid field name.
<二>Python的List和Matlab的Cell Array比较
两者的重要不同:Python的List的下标从0开始,Matlab的Cell Array的下标从1开始。
>>> li = ["a", "b", "mpilgrim", "z", "example"]
>>> li
['a', 'b', 'mpilgrim', 'z', 'example']
>>> li[0]
'a'
>>> li[4]
'example'
>> li = {'a', 'b', 'mpilgrim', 'z', 'example'}
li =
>> li(1)
ans =
>> li(5)
ans =
>>> li[0:3]
['a', 'b', 'mpilgrim']
>> li(1:3)
ans =
>>> li[:3]
['a', 'b', 'mpilgrim']
>>> li[3:]
['z', 'example']
>> li(1:3)
ans =
>> li(4:end)
ans =
>>> li = ['a', 'b', 'c']
>>> li.extend(['d', 'e', 'f'])
>>> li
['a', 'b', 'c', 'd', 'e', 'f']
>>> len(li)
6
>> li={'a','b','c'}
li =
>> li=[li {'d','e','f'}]
li =
>> numel(li)
ans =
>>> li = ['a', 'b', 'c']
>>> li.append(['d', 'e', 'f'])
>>> li
['a', 'b', 'c', ['d', 'e', 'f']]
>>> len(li)
4
>>> li[-1]
['d', 'e', 'f']
>> li={'a','b','c'}
li =
>> li{end+1}={'d','e','f'}
li =
>> numel(li)
ans =
>> li{end}
ans =
>>> k = "uid"
>>> v = "sa"
>>> "%s=%s" % (k, v)
'uid=sa'
>> k='uid';
>> v='sa';
>> sprintf('%s=%s',k,v)
ans =
uid=sa
>>> li = [1, 9, 8, 4]
>>> [elem*2 for elem in li]
[2, 18, 16, 8]
>> li={1,9,8,4};
>> numli=zeros(1,numel(li));
>> for n=1:numel(numli)
numli(n)=li{n};
end
>> numli*2
ans =
>>> params = {"server":"mpilgrim", "database":"master", "uid":"sa"}
>>> li=["%s=%s" % (k, v) for k, v in params.items()]
>>> li
['database=master', 'uid=sa', 'server=mpilgrim']
>> li=[]
li =
>> params = struct('server','mpilgrim', 'database','master', 'uid','sa')
params =
>> names=fieldnames(params);
>> for n=1:numel(names)
li{n}=sprintf('%s=%s',names{n},params.(names{n}));
end
>> li
li =
>>> g = lambda x: x*2
>>> g(3)
6
>> g=@(x)x*2;
>> g(3)
ans =
>>> (lambda x: x*2)(3)
6
>> @(x)x*2(3)
??? @(x)x*2(3)
Error: Unbalanced or misused parentheses or brackets.
最后
以上就是重要天空为你收集整理的Matlab与Python的语法比较的全部内容,希望文章能够帮你解决Matlab与Python的语法比较所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复