當前位置:首頁 » 編程語言 » python277

python277

發布時間: 2023-03-03 17:43:27

㈠ 求python代碼編動態規劃貝爾曼函數

classNode(object):

def__init__(self,name):
self._name=name
self._value=None
self._from=None
self._next=[]

defsetValue(self,value):
self._value=value

defsetNext(self,node):
self._next.append(node)

defsetFrom(self,node):
self._from=node

defgetValue(self):
returnself._value

defgetNext(self):
returnself._next

defgetName(self):
returnself._name

defgetFrom(self):
returnself._from

classBFtree():

def__init__(self,dList):
self._dList=dList

defbfValue(self,start):
cur=start
ifcur.getNext()isnotNone:
fornodeincur.getNext():
path=[cur.getName(),node.getName()]
path.sort()
path=''.join(path)
value=cur.getValue()+self._dList[path]
ifnode.getValue()isNoneorvalue<node.getValue():
node.setValue(value)
node.setFrom(cur)
self.bfValue(node)

defmove(self,start,end):
print'From:',start.getName(),'to:',end.getName()
start.setValue(0)
self.bfValue(start)
trace=[end.getName()]
cur=end
whilecur.getFrom()isnotNone:
cur=cur.getFrom()
trace.append(cur.getName())
trace=reversed(trace)
print'Thepathis',''.join(trace),'andthevalueis',end.getValue()

#builidngnode
a=Node('A')
b=Node('B')
c=Node('C')
d=Node('D')
e=Node('E')
f=Node('F')
g=Node('G')
h=Node('H')

#buildtree
a.setNext(b)
a.setNext(c)
a.setNext(d)

b.setNext(a)
b.setNext(g)

c.setNext(a)
c.setNext(g)
c.setNext(e)

d.setNext(a)
d.setNext(e)

e.setNext(c)
e.setNext(b)
e.setNext(f)

f.setNext(e)

g.setNext(b)
g.setNext(c)
g.setNext(h)

h.setNext(g)

#builddistancelist
dList=dict();
dList['AB']=1
dList['AC']=2
dList['AD']=3
dList['BG']=1
dList['BE']=6
dList['CE']=2
dList['CG']=5
dList['DE']=4
dList['EF']=3
dList['GH']=4

#buildBFtree
tree=BFtree(dList)
tree.move(a,h)
tree.move(a,f)
tree.move(a,e)

代碼略長....

㈡ python文件中讀取某一列數據然後輸出數組匹配到另外一個文件中

我可以認為你是准備用這個文件第五列數據替換另一個文件的第五列數據么?

#下面的split裡面我些的是空格,若不是空格分割,請替換
data1 = map(lambda x:x.strip().split(" ")[4],open("oct4.txt").readlines())
#這里我假設你要替換的文件是oct5.txt
data2 = map(lambda x:x.strip().split(" "),open("oct5.txt").readlines())

data2_new = []
for line in data2:
line[4] = data1.pop(0)
data2_new.append(line)
file("oct5_new.txt"."w").writelines(data2_new)

熱點內容
怎麼用電腦開手機伺服器 發布:2024-11-07 10:30:50 瀏覽:501
代掛網源碼最新版 發布:2024-11-07 10:28:30 瀏覽:820
atoilinux 發布:2024-11-07 10:19:06 瀏覽:462
速騰哪個配置性能好 發布:2024-11-07 09:55:08 瀏覽:622
壓縮空氣的價格 發布:2024-11-07 09:51:25 瀏覽:541
達內培訓班java 發布:2024-11-07 09:51:19 瀏覽:315
c語言快速排序函數 發布:2024-11-07 09:41:16 瀏覽:210
mobisage文件夾 發布:2024-11-07 09:29:16 瀏覽:877
車載安卓機怎麼清理垃圾 發布:2024-11-07 09:12:51 瀏覽:451
外網訪問vmware 發布:2024-11-07 08:51:03 瀏覽:953