Question

I'm trying to modify a python script that models a version of Conway's game of life. In which a set of cells in X number of columns and Y number of rows are each given a value that determines if they will switch between two states of active or dormant depending on the state of their neighbors.

Right now the initial values for these cells are defined by this definition, which references a number of variables set by the user. (this isnt the full script, just what I think is relevant)

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()

What I would like to do is replace the random.random function with a .txt file of values that I can set myself.

This is what I have come up with so far

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

But, when this is run it gives me an error saying "Message: unsupported operand type(s) for +: 'int' and 'list'"

The .txt file is set up like this [.1,.1,.9,.9,.... etc]

Any suggestion on why this is happening? And the full code can be found here. https://stackoverflow.com/questions/22138217/assistance-with-python-gol-script

No correct solution

OTHER TIPS

Your description of the text file format is rather sketchy, so I will assume that each row is represented with a starting and ending square bracket and the individual values are separated by commas with optional whitespace around them. This means the first character and the last two (there's a newline, too) should be ignored. My file looked like this:

[1, 3, 5, 7, 9]
[3, 5, 7, 9, 11]
[5, 7, 9, 11, 13]

The following code generates an array according to the dimension of the file rather than using set values, but hopefully this will get you on your way.

 def selectedArray(intLength,intWidth):
    arr = []
    selected = open('ArrayValues2.txt','r')
    for line in selected:
        values = [float(f.strip()) for f in line[1:-2].split(',')]
        arr.append(values)
    return arr  

for row in selectedArray(3, 5):
    print(row)

The output is

[1.0, 3.0, 5.0, 7.0, 9.0]
[3.0, 5.0, 7.0, 9.0, 11.0]
[5.0, 7.0, 9.0, 11.0, 13.0]

First, you definitely do not want to open your text file inside your loop. Before you start your loop, open the text file and read the data into a variable.

Second, it doesn't look like the error you are seeing is in the section of code you have posted, but it is likely due to not correctly parsing the data from your file. You are .appending a whole list of items when you should be appending a single number.

If the file is just a single line of comma-separated values (with [] on the end), then you need to strip the [], split on the commas (as you did), and convert each string to a float. You then need to add the items one at a time instead of all at once.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top