>>> c = collections.Counter(a=23, b=-9)
#You can add a new element and set its value like this:
>>> c['d'] = 8
>>> c
Counter({'a': 23, 'd': 8, 'b': -9})
#Increment:
>>> c['d'] += 1
>>> c
Counter({'a': 23, 'd': 9, 'b': -9}
#Note though that c['b'] = 0 does not delete:
>>> c['b'] = 0
>>> c
Counter({'a': 23, 'd': 9, 'b': 0})
#To delete use del:
>>> del c['b']
>>> c
Counter({'a': 23, 'd': 9})
Counter is a dict subclass
Usefloatto convertstringvalue tofloat. OR We can useeval()also.
Use listappend()method to append tuple to list.
Use try except to prevent code from break.
My text file is:
-34.968398,-6.487265
-34.969448,-6.488250
-34.967364,-6.492370
-34.965735,-6.582322
Code:
p = "/home/vivek/Desktop/test.txt"
result = []
with open(p, "rb") as fp:
for i in fp.readlines():
tmp = i.split(",")
try:
result.append((float(tmp[0]), float(tmp[1])))
#result.append((eval(tmp[0]), eval(tmp[1])))
except:pass
print result
Output:
$ python test.py
[(-34.968398, -6.487265), (-34.969448, -6.48825), (-34.967364, -6.49237), (-34.965735, -6.582322)]