By dictionary:
- Read txt file by
csv module because file have well define structure.
- Create dictionary from the file content where key is name and value is integer number i.e. score
- Add new score for existing student.
- Create new entry for new sequent.
Demo:
import csv
import pprint
p = "input34.txt"
with open(p, "rb") as fp:
root = csv.reader(fp, delimiter=',')
result = {}
for i in root:
result[i[0].strip()] = int(i[1].strip())
print "Debug1 result:"
pprint.pprint (result)
serach_key = "Test student"
add_value = 5
if serach_key in result:
result[serach_key] = result[serach_key] + add_value
else:
result[serach_key] = add_value
print "Debug2 result:"
pprint.pprint (result)
serach_key = "Another Student"
add_value = 5
if serach_key in result:
result[serach_key] = result[serach_key] + add_value
else:
result[serach_key] = add_value
print "Debug3 result:"
pprint.pprint (result)
Output:
vivek@vivek:~/Desktop/stackoverflow$ python 34.py
Debug1 result:
{'Another Student': 1,
'Emily Scott': 3,
'Student Name': 2,
'Third Student': 10}
Debug2 result:
{'Another Student': 1,
'Emily Scott': 3,
'Student Name': 2,
'Test student': 5,
'Third Student': 10}
Debug3 result:
{'Another Student': 6,
'Emily Scott': 3,
'Student Name': 2,
'Test student': 5,
'Third Student': 10}
By list:
Demo:
mport csv
import pprint
p = "input34.txt"
with open(p, "rb") as fp:
root = csv.reader(fp, delimiter=',')
result = []
for i in root:
result.append([i[0].strip(), int(i[1].strip())])
print "Debug1 result:"
pprint.pprint (result)
serach_key = "Test student"
add_value = 5
add_flag = False
for i,j in enumerate(result):
if serach_key==j[0]:
j[1] = j[1] + add_value
add_flag = True
break
if add_flag==False:
result.append([serach_key, add_value])
print "Debug2 result:"
pprint.pprint (result)
serach_key = "Another Student"
add_value = 5
add_flag = False
for i,j in enumerate(result):
if serach_key==j[0]:
j[1] = j[1] + add_value
add_flag = True
break
if add_flag==False:
result.append([serach_key, add_value])
print "Debug3 result:"
pprint.pprint (result)
Output:
vivek@vivek:~/Desktop/stackoverflow$ python 34.py
Debug1 result:
[['Emily Scott', 3],
['Student Name', 2],
['Another Student', 1],
['Third Student', 10]]
Debug2 result:
[['Emily Scott', 3],
['Student Name', 2],
['Another Student', 1],
['Third Student', 10],
['Test student', 5]]
Debug3 result:
[['Emily Scott', 3],
['Student Name', 2],
['Another Student', 6],
['Third Student', 10],
['Test student', 5]]
Use collection.defaultdict to optimize code. So no need to check key is in dictionary or not.
e.g.:
>>> import collections
>>> result = collections.defaultdict(int)
>>> result["student 1"] = 10
>>> result["student 2"] = 20
>>> print result
defaultdict(<type 'int'>, {'student 1': 10, 'student 2': 20})
>>> result["student 2"] = result["student 2"] + 2
>>> print result
defaultdict(<type 'int'>, {'student 1': 10, 'student 2': 22})
>>> result["student 3"] = result["student 3"] + 5
>>> print result
defaultdict(<type 'int'>, {'student 1': 10, 'student 2': 22, 'student 3': 5})
>>>
collections.defaultdict), not a list.