0
def update(all_marks, stud_num, mark, column, result):
    lines = [l for l in all_marks]

    for row in all_marks:
        if stud_num in row:
            lines[rows][column] = mark

From here, I am trying to replace the value by using lines[rows][column] = mark.

It is supposed to replace the existing value with mark. But there's a problem with defining rows. Anyone knows how to fix? Thanks.

Edit: Here's sample of data from all_marks:

[['a', '', '', '', '', ''],

['b', '', '', '', '', ''],

['c', '', '', '', '', ''],

['d', '', '', '', '', ''],

['e', '', '', '', '', ''],

['f', '', '', '', '', ''],

['g', '', '', '', '', '']]

What I want to do here is to replace the value in '' with mark.

For example, def update(all_marks, 'a', '10', 2, True): will return

[['a', '', '10', '', '', ''],

['b', '', '', '', '', ''],

['c', '', '', '', '', ''],

['d', '', '', '', '', ''],

['e', '', '', '', '', ''],

['f', '', '', '', '', ''],

['g', '', '', '', '', '']]

Thanks for helping a newbie.

4
  • 2
    Please take care to format your code properly. The one that you posted won't even run. Also what exact problem do you encounter? Commented Aug 19, 2012 at 7:29
  • 2
    would you be able to post a sample of data from all_marks? Commented Aug 19, 2012 at 7:30
  • Did you try the Python csv modul? It works fine for tasks like yours. Commented Aug 19, 2012 at 8:07
  • @Dreen: Thanks for a comment. I've uploaded a sample. Commented Aug 19, 2012 at 10:04

3 Answers 3

2

Here is a modified version of your function that will return the output as expected:

def update(all_marks, stud_num, mark, column):
    for i in range(len(all_marks)):
        if stud_num in all_marks[i]:
            all_marks[i][column] = mark
    return all_marks

And here is how it works:

>>> marks
[['a', '', '', '', '', ''], ['b', '', '', '', '', ''], ['c', '', '', '', '', ''], ['d', '', '', '', '', ''], ['e', '', '', '', '', ''], ['f', '', '', '', '', ''], ['g', '', '', '', '', '']]

>>> update(marks,'a','10',2)
[['a', '', '10', '', '', ''], ['b', '', '', '', '', ''], ['c', '', '', '', '', ''], ['d', '', '', '', '', ''], ['e', '', '', '', '', ''], ['f', '', '', '', '', ''], ['g', '', '', '', '', '']]

Note that marks is now modified

>>> marks
[['a', '', '10', '', '', ''], ['b', '', '', '', '', ''], ['c', '', '', '', '', ''], ['d', '', '', '', '', ''], ['e', '', '', '', '', ''], ['f', '', '', '', '', ''], ['g', '', '', '', '', '']]

If you want to change that so that update simply returns a copy of modified data change the function in the following way:

def update(all_marks, stud_num, mark, column):
    tmp = all_marks
    for i in range(len(tmp)):
        if stud_num in tmp[i]:
            tmp[i][column] = mark
    return tmp
Sign up to request clarification or add additional context in comments.

1 Comment

I initially posted the code and just updated my answer to give you some more information
1

Here is the working code:

def update(all_marks, stud_num, mark, column, result):
    lines = [l for l in all_marks]
    for row in range(len(all_marks)):
        if all_marks[row][0] == stud_num:
            lines[row][column] = mark

And here are the explanations:

for row in range(len(all_marks)):

=> you don't want to iterate over list objects (e.g. ['a','','','','','']) but over list indices

if stud_num == all_marks[row][0]:

=> This is to check only the first character of your row, not any character.

lines[row][column] = mark

=> typo here, should be row and not rows

1 Comment

Dreen beat me up for a few seconds on this one :)
0

Looping on the row indices isn't as efficient as looping on the rows themselves. A more pythonic way would be

def update2(all_marks,stud_num,mark,column):
    for row in all_marks:
        if stud_num in row:
            row[column] = mark
    return all_marks

Comments

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.