I have a file as such:
1 23
2 21
5 23
561 2
73 19781
And with this function:
def readx(x):
return {int(line.split()[0]):int(line.split()[1]) for line in x.split('\n')}
I can get this:
{1: 23, 2: 21, 5: 23, 73: 19781, 561: 2}
But I need to put it into some sort of class object, so I tried this:
z = """1 23
2 21
5 23
561 2
73 19781"""
def readx(x):
return {int(line.split()[0]):int(line.split()[1]) for line in x.split('\n')}
class Foo(dict):
def __init__(self, x):
self = readx(x)
f = Foo(z)
print f
But it returns a None instead of the dictionary.
- Is there a more pythonic way to do
readx()
? It a little ugly as it is now. - How do i get the class object to work and make
foo
a dict with keys and values?