0

I am trying to modify data in a 2 dimensional matrix (represented by a sequence of smaller sequences) in python. The matrix values are first initialized to "0.0" (shown as "current" in code below). I have a second 2 dimensional matrix that contains the actual data points, however, these data points are represented by ".", "C", and "H". I have written the code below to basically translate these characters into the proper values, but I'm running into an error "str object does not support item assignment". I know strings are immutable, but I'm not changing individual characters of a string, I'm removing the whole string. Here is the code:

for rline in range(len(lineAppend)):
    for cline in range(len(lineAppend)):
        if lineAppend[rline][cline]==".":
            #print "is dot"
            lineAppend[rline][cline] == 0.0
            #lineAppend.remove(".")
            #ineAppend.insert("0.0")
        elif lineAppend[rline][cline]=="C":
            lineAppend[rline][cline] == 25.0
        elif lineAppend[rline][cline]=="H":
            lineAppend[rline][cline] == 100.0

lineAppend is a matrix that is filled by reading a text file. It simply reads each line and saves it as a sequence. I am trying to change individual indexes to the above values, but I keep running into the error that occurs because strings are immutable. Is there any other way to modify the data to the desired values?

Here is the traceback:

    Traceback (most recent call last):
    File "/home/Desktop/python_projects/temp.py", line 172, in <module>
main()
    File "/home/Desktop/python_projects/temp.py", line 169, in main
read_config("plate.txt")
    File "/home/Desktop/python_projects/temp.py", line 110, in read_config
    if lineAppend[rline][cline]==".":

TypeError: list indices must be integers, not str

1
  • 2
    There is no assignment taking place here; can you show us the full traceback? Commented Oct 18, 2013 at 17:48

2 Answers 2

2

You are mixing equality tests with assignment:

if lineAppend[rline][cline]==".":
    lineAppend[rline][cline] == 0.0

Note how both lines use == to test for equality. Assignment, on the other hand, uses one =:

if lineAppend[rline][cline] == ".":
    lineAppend[rline][cline] = 0.0

You are doing this throughout your code; if you want to assign, use a single equals symbol.

Also, to loop over your matrix, you can loop directly. You can include an index when you need to assign to the inner lists:

for rline in lineAppend:
    for i, cline in enumerate(rline):
        if cline == ".":
           rline[i] = 0.0
        elif cline == "C":
           rline[i] = 25.0
        elif cline == "H":
           rline[i] = 100.0

If you were to use a mapping, the whole nested loop can be replaced by a list comprehension:

replacements = {'.': 0.0, 'C': 25.0, 'H': 100.0}
lineAppend = [[replacements.get(v, v) for v in rline] for rline in lineAppend]
Sign up to request clarification or add additional context in comments.

Comments

1
lineAppend[rline][cline] == 0.0

is not assigning value, but it is simply comparing. Change it to

lineAppend[rline][cline] = 0.0

3 Comments

When I change it to an assignment operator I still get the "str does not support item assignment" error
@JeremyFisher: You'll need to show us the full traceback; your code, as posted, doesn't assign anything so it cannot be the cause of the exception.
I just added the traceback. Even after changing the double equal to single equal I still keep getting the error, so does that mean it could be an assignment error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.