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.
-
2Please take care to format your code properly. The one that you posted won't even run. Also what exact problem do you encounter?Ihor Kaharlichenko– Ihor Kaharlichenko2012年08月19日 07:29:50 +00:00Commented Aug 19, 2012 at 7:29
-
2would you be able to post a sample of data from all_marks?Dreen– Dreen2012年08月19日 07:30:31 +00:00Commented Aug 19, 2012 at 7:30
-
Did you try the Python csv modul? It works fine for tasks like yours.MaxPowers– MaxPowers2012年08月19日 08:07:07 +00:00Commented Aug 19, 2012 at 8:07
-
@Dreen: Thanks for a comment. I've uploaded a sample.Kevvv– Kevvv2012年08月19日 10:04:36 +00:00Commented Aug 19, 2012 at 10:04
3 Answers 3
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
1 Comment
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
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