我試圖修改一個模擬康威的生命遊戲版本的python腳本。其中,X列和Y行中的一組單元格各自被賦予一個值,該值確定它們是否將根據它們的鄰居的狀態在活動或休眠的兩種狀態之間切換。Python:用.txt列表替換random.random
現在,這些單元格的初始值由此定義定義,該定義引用用戶設置的多個變量。 (這個心不是完整的劇本,只是我認爲是相關的)
def randomizeArray(intLength,intWidth):
arr = []
for j in range(intWidth):
arri = []
for i in range(intLength):
rnd =random.random()
arri.append(rnd)
arr.append(arri)
print rnd
return arr
def Main():
intLength = rs.GetInteger("how many in x",30)
intWidth = rs.GetInteger("how many in y",30)
intGen = rs.GetInteger("how many generations",40)
strStack = rs.GetString ("should I stack the generations", "yes", ["yes", "no"])
crvs = rs.GetObjects("select the crvs",4)
thres = rs.GetReal("type the threshold to voxelize",1)
allValues = []
arrValues = randomizeArray(intLength,intWidth)
for i in range(40):
arrValues = applyGOL(arrValues)
allValues.append(arrValues)
#arrMeshes = render(arrValues,-1, strStack)
for i in range(intGen-1):
arrValues = applyGOLCrvs(arrValues, i, crvs)
allValues.append(arrValues)
"""
if strStack == "no" :
update(arrMeshes, arrValues)
else :
render(arrValues,i, strStack)
"""
myVoxels = voxels(intLength,intWidth,intGen, allValues)
myVoxels.voxelize(thres)
#Call DeleteObjects2dArray(arrMeshes)
Main()
我想要做的是,我可以給自己設定值的.txt文件替換random.random功能。
這就是我來了這麼遠
def selectedArray(intLength,intWidth):
arr = []
for j in range(intWidth):
arri = []
for i in range(intLength):
selected = open('C:\Users\IAmADog\Documents\Thesis\Scripts\ArrayValues2.txt','r')
lines = selected.read().split(',')
arri.append(lines)
arr.append(arri)
return arr
但是,當這種運行它給了我一個錯誤說「消息:不支持的操作數類型(S)爲+:‘詮釋’和'名單'」
.txt文件設置這樣[.1 .1 .9 .9 ....等]
上爲什麼會發生什麼建議嗎? 完整的代碼可以在這裏找到。 https://stackoverflow.com/questions/22138217/assistance-with-python-gol-script
可能重複(http://stackoverflow.com/questions/22138217/assistance-with-python-gol-script) – 2rs2ts
你能發佈一個.txt文件的例子嗎? (是多行,還是單行顯示)。你用手創造它嗎?爲什麼有[]在裏面? – bj0
它被格式化爲範圍從0到1的數字列表。每個數字後面都有一個逗號。文件中沒有括號。它看起來像這樣:「.1,1,1,1,1,.9,.9,.9,.9,.9,.1,.1,.1,.1,.1, .9,.9,.9,.9,.9,.1,.1,.1,.1,.1,.9,.9,.9,.9,.9「 – HelloThisIsDog